Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/* ----------------------------------------------------- */
/* AEDE 6120 */
/* Lab 2 SAS Basics */
* A few things to notice:
- remember to put a semicolon after your command
- remember to put a RUN after your step
- case and format of the code do not matter
- two ways to add comments;
* lab preparation:
- create the 'aede' folder
- upload 'CLASS1.csv' and 'CLASS2.xlsx'
- create a sas program;
/* ----------------------------------------------------- */
/* Set library path */
libname aede "/home/u49260161/aede";
/* Import data files */
* example 1: import a csv file as a sas table;
PROC IMPORT DATAFILE="/home/u49260161/aede/CLASS1.csv"
DBMS=csv
OUT=aede.class1;
GETNAMES=yes;
RUN;
* example 2: import a xlsx file as a sas table;
PROC IMPORT DATAFILE="/home/u49260161/aede/CLASS2.xlsx"
DBMS=xlsx
OUT=aede.class2;
GETNAMES=yes;
RUN;
/* Print datasets */
PROC PRINT DATA=aede.class1;
RUN;
/* Export data files */
* export a sas table as a xlsx file;
PROC EXPORT DATA=aede.class1 OUTFILE="/home/u49260161/aede/class1x.xlsx"
DBMS=xlsx
REPLACE;
RUN;
/* Access SAS tables */
DATA work.class1;
SET aede.class1;
RUN;
/* Filter rows & select columns */
DATA work.class1filt;
SET aede.class1;
WHERE age >= 13 AND sex = 'F';
RUN;
DATA work.class1keep;
SET aede.class1;
KEEP name sex age;
RUN;
DATA work.class1drop;
SET aede.class1;
DROP sex age;
RUN;
/* Create new variables */
DATA work.class1newv;
SET aede.class1;
new_height = height * 2 - 50;
new_weight = weight + 20;
RUN;
/* Sort */
PROC SORT DATA=aede.CLASS1 OUT=work.class1s;
BY age name;
RUN;
PROC SORT DATA=aede.CLASS1 OUT=work.class1s;
BY age descending height;
RUN;
/* Merge */
PROC SORT DATA=aede.CLASS1 OUT=work.sort1;
BY Name;
RUN;
PROC SORT DATA=aede.CLASS2 OUT=work.sort2;
BY Name;
RUN;
DATA aede.class_merge;
MERGE sort1 sort2; *note: 'work' is optional;
BY Name;
RUN;
/* Delete */
PROC DELETE DATA=sort1 sort2;
RUN;