Skip to content

Instantly share code, notes, and snippets.

@abhishekdagarit
Last active November 11, 2017 16:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abhishekdagarit/4ac3e3498261ab267e4f7e0a581de521 to your computer and use it in GitHub Desktop.
Save abhishekdagarit/4ac3e3498261ab267e4f7e0a581de521 to your computer and use it in GitHub Desktop.
SAS lecture1

#Printing datasets in SAS

proc print data=work.intro;
run;

Where clause

proc print data=work.intro;
where rating = 2;
run;

Spaces don't matter

proc print data=work; where rating=2;
run;


proc    print    data=work.
intro;
  where rating = 2;
run

Not case sensitive

proc PrINT data=Work.InTRo;
RuN;

Case sensitive only while comparing

proc print data=work.intro;
where post="ASE";
run;

Printing only one variable

proc print data=work.intro;
print var;
run;

Steps in SAS

Data step

data work.test;
x=12;
y=32;
z=212;
run;
proc print data=work.test;
run;

Print step

proc print data=work.test;
run;

Datalines

Comma is not valid as a variable separator in datalines.

data work.test;
input x y;
datalines;
3 4 
23 5
232 5
23 5
;
run;

proc print data=work.test;
run;

In the above code: input x, y would have resulted in an error.

Demiliter

Space is a default delimiter in datalines

Datalines, reads the data and moves to the next line.

data work.test;
input x y;
datalines;
3 4 
23 5
232 5
23      5
;
run;

proc print data=work.test;
run;

Less variables but more data

Here it will take the first two lines only as space is the delimiter for SAS.

data work.test;
input x y;
datalines;
1 3 3  
5 456 8
232 5 44
23 5 64
;
run;

proc print data=work.test;
run;

Variables are more but data is less

Assignment gets tricky in this way.

Speculation: It will take values for all or it will leave blank.

data work.test;
input x y z;
datalines;
1 3 3  
5 456 8
232 5 44
23 5 64
;
run;

proc print data=work.test;
run;

Another case like the last one

Here, it won't assign anything, just will leave it blank.

data test;
inout x y;
datalines;
1
;
run;

proc print data=test;
run;

To input a data file

data work.test;
infile `path_to_file.txt';
input x y;
run;

proc print data=work.test;
run;

Data types

Character and num

Characters end with a dollar sign.

data work.test;
infile `path_to_file.txt';
input name$ age salary;
run;

proc print data=work.test;
run;

Can't perform operations on a numeric variable when saved as a character.

So can't perform operations on age

data work.test;
infile `path_to_file.txt';
input name$ age$ salary;
run;

proc print data=work.test;
run;

Lecture 2.1

Menas

General syntax

Prints all

proc print data=work.test;
run;

Means of only one variable

proc means data=work.test;
var age;
run;

Means of sorted variables

This will give error as data has to be sorted first.

proc means data=work.test;
var age;
by post;
run;
proc sort data=work.test;
run;

Now it will work.

proc means data=work.test;
var age;
by post;
run;

Maximum decimal points

proc means data=work.test maxdesc=2;
var age;
by post;
run;

Print only means

proc means data=work.test means;
var age;
by post;
run;

Print means min max std

proc means data=work.test mean min max std;
var age;
by post;
run;

Print means min max std median sum

proc means data=work.test mean min max std median sum;
var age;
by post;
run;

Sort

Class - alternative of sort

Doesn't require data to be sorted first.

proc means data=work.test;
var age;
class post;
run;

Comments in SAS

* comments in sas;
\* both of these are multi
line comments */
proc print data=work.test;
run;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment