Professional Documents
Culture Documents
Sas Sample Syntax
Sas Sample Syntax
set sasuser.admit;
run;
NEW_FEE = 10 * FEE;
BMI = WEIGHT/HEIGHT;
RUN;
/* KEEP STATEMENT */
RUN;
RUN;
/* RENAME */
DATA NEW2;
SET ADMIT (rename=(age=new_age));
myage= new_age*2;
RUN;
/* _n_ */
/*gt lt ge le eq*/
/*> < >= <= =*/
/* USING CONDITIONS*/
set admit;
run;
/*WHERE STATEMENT */
run;
data new7 ;
LENGTH GENDER $ 8;
set admit(where=(age gt 30));
if sex='M' then gender='MALE';
if sex='F' then gender='FEMALE';
drop sex;
run;
data new7;
LENGTH GENDER $ 8;
/*null statement */
data _null_;
a='26oct02'd;
put 'SAS date=' a;
put 'formatted date=' a date9.;
run;
SET SASUSER.ADMIT;
RUN;
LIBNAME ;
RUN;
DATA CLASS;
INPUT NAME $ SEX $ AGE HEIGHT WEIGHT;
DATALINES;
JOHN
M 12 59.0 99.5
JAMES
M 12 57.3 83.0
RUSHI
M 24 60
200
RUN;
DATA cars1;
LENGTH PRICE 8 ;
INPUT make $ model $ mpg weight price;
datalines;
AMC Concord 22 2930 4099
AMC Pacer
17 3350 4749
DATA cars2;
INPUT make $ 1-5 model $ 6-12 mpg 13-14 weight 15-18 price 19-22;
CARDS;
AMC Concord2229304099
AMC Pacer 1733504749
AMC Spirit 2226403799
BuickCentury2032504816
BuickElectra1540807827
;
RUN;
DATA cars3;
17
AMC Spirit 22
Bui Century 20
Bui Electra 15
;
RUN;
DATA NEW2;
DATALINES;
101FRANKM27
1208017878
101FRANKM27
1208017878
101FRANKM27
1208017878
101FRANKM27
1208017878
101FRANKM27
1208017878
;
RUN;
data cars4;
run;
data cars4;
run;
data date_val;
input date;
datalines;
01JAN1960
12Apr2011
15jun2012
03mar2010
01JAN1950
;
run;
data scores;
input name$ 12. +4 score1 $
COMMA8.2;
datalines;
Riley
1,132
Henderson
1,015
1,18754
$1000.56
1,10254
$2000.50
RUN;
DATA NEW1;
DATALINES;
123456A
123456B
123456C
123456D
;
RUN;
data weather;
input temp1-temp5;
datalines;
96 95 98
98 98 99 95 96
98 78 54 85 85
;
run;
proc print data=weather;
run;
data add;
datalines;
"steve",32,"moanaparis"
;
run;
title ;
proc print data=add noobs ;
run;
/*YEAR CUTOFF */
options yearcutoff=1910;
data new1;
date = "01JAN19"d;
put date = date9.;
date1 = "01JAN20"d;
put date1 = date9.;
run;
data new2 ;
set new1;
format date date9.;
format date1 date9.;
run;
/*CONTENTS*/
title;
PROC CONTENTS DATA = new2 ;
RUN;
/*FUNCTIONS */
*hjbfjbfeb;
DATA getdata;
Chris Adraktas
John Brown
. 79.7 79.07 72
;
RUN;
DATA funct1;
SET getdata;
t1int = INT(test1);
t2int = INT(test2);
t1rnd = ROUND(test1);
t2rnd = ROUND(test2,100); /* round to nearest whole number */
tavg = MEAN(test1, test2, test3);
RUN;
DATA funct2;
SET getdata;
xsqrt = SQRT(x);
/* square root */
xlog = LOG(x);
/* log base e */
xexp = EXP(x);
RUN;
RUN;
DATA NEW1;
INFILE DATALINES;
INPUT A1-A6 ;
DATALINES;
123456
123456
123456
123456
;
RUN;
DATA NEW2;
SET NEW1;
X1 = MAX (OF A1-A6);
X2 = MIN (OF A4-A6);
RUN;
/*Arrays*/
Data original;
Input x1-x5;
datalines;
9878.
876.9
..976
;
run;
proc print;
Title 'Original Data';
run;
data modified;
set original;
if x1=. then x1=0;
if x2=. then x2=0;
if x3=. then x3=0;
if x4=. then x4=0;
if x5=. then x5=0;
run;
title 'modified data';
proc print;run;
data modified;
set original;
array zero x1-x5;
do over zero;
if zero=. then zero=0;
end;
run;
proc print;
Title 'Data modified with arrray and do loop';
run;
DATA ftoc2;
INPUT month $ f1-f10;
ARRAY f{10} f1-f10;
ARRAY c{10} c1-c10;
DO i=1 to 10;
c{i}=( f{i}-32 )*5/9;
END;
FORMAT c1-c10 4.1;
DATALINES;
aug
94 98 99 98 99 96 91 90 88 89
sept
93 92 87 87 89 90 91 92 82 80
;
PROC PRINT;
title1 'DATA; FTOC2';
title2 'Explicit Array Example';
RUN;
DATA ftoc;
INPUT month $ f1-f10;
ARRAY f(i) f1-f10;
ARRAY c(i) c1-c10;
DO over f;
c=(f-32)*5/9;
END;
FORMAT c1-c10 4.1;
DATALINES;
aug
94 98 99 98 99 96 91 90 88 89
sept
93 92 87 87 89 90 91 92 82 80
;
PROC PRINT;
TITLE1 'DATA: FTOC';
TITLE2 'Implicit Array Example';
run;
DATA ADMIT;
SET SASUSER.ADMIT;
RUN;
TITLE;
proc print data =admit;
run;
/* SORTING DATASET */
data new2;
set sasuser.admit;
run;
proc print;run;
DATA getdata;
INPUT name $14. x test1 test2 test3;
DATALINES;
John Smith
Samuel Adams
Samuel Adams
Chris Adraktas
Samuel Adams
;
RUN;
run;
data new2;
set sasuser.admit;
run;
proc print data = new2
;run;
actlevel sex;
RUN;
run;
data new5;
set new4;
by sex ;
if first.sex then sum_fee=0;
sum_fee+fee;
/* if last.sex then wt=1; */
new_var=sum(height,weight);
run;
run;
data new5;
set new4;
by sex ;
if first.sex then flag1='A';
if last.sex then flag2='B';
run;
/* SUM FUNCTION */
data newq;
input a b c;
datalines;
123
456
8.5
12.
456
895
;
run;
data m;
set newq;
new=sum( a,b,c);
new1=a+ b + c;
data lb;
input subject visit test$ value;
datalines;
101 1 CBP 12
101 2 CBP 25
101 3 CBP 29
101 4 CBP 30
102 3 CBP 12
102 2 CBP 25
103 7 CBP 29
103 5 CBP 30
101 1 AST 12
101 2 AST 25
101 3 AST 29
101 4 AST 30
102 3 AST 12
102 2 AST 25
103 7 AST 29
103 5 AST 30
;
run;
data lb_1;
set lb1;
run;
data new;
string="
ZOOLOGY
";
L1 = LENGTH (STRING);
L2 = LENGTHC(STRING);
var1= left(string);
l3=length(var1);
l4=length(var2);
var2= right(string);
var3 = trim(string);
var4=trim(LEFT(string));
var5=compress (string);
l5=length(var3);
l6=length(var4);
l7=length(var5);
run;
proc print data=new;
run;
/*CONCATENATION*/
run;
proc print data=new;
run;
/*SUBSTR*/
data new;
name="Frankenstein";
run;
proc print data=new;
run;
data new1;
set new;
nick_name= substr(name,1,5);
run;
proc print data=new1;
RUN;
/*SCAN*/
data k;
string="my name is khan";
a1=scan(string,1);
a2=scan(string,2);
a4=scan(string,-1);
output;
run;
data k;
string="my name is, khan";
a1=scan(string,1,',');
a2=scan(string,2,',');
output;
run;
proc print data=k;
run;
/*TRANWRD*/
data new;
address = "#203, south king drive street, Chicago";
new= tranwrd(address,"street","St.");
run;
proc print data=new;
run;
/*INDEX*/
data new;
string="How much WOOD would a woodchuck chuck";
new1=index(string,"WOOD");
new2=index(string,"wood");
run;
proc print data=new;
run;
/*FIND*/
data new;
string="How much WOOD would a woodchuck chuck";
new1=find(string,"CHUCK",'it',30);
run;
proc print data=new;
run;
data admit;
set sasuser.admit;
run;
data new;
set admit;
if (actlevel="HIGH" or actlevel="LOW") and sex="F";
/*if actlevel in ("HIGH","LOW");*/
run;
proc print data=new;
run;