0% found this document useful (0 votes)
92 views34 pages

Sas Sample Syntax

The document shows SAS code for various data manipulation tasks like creating new datasets from existing datasets, modifying and transforming variables, sorting and merging datasets, and applying functions. Key operations demonstrated include selecting/dropping variables, calculating new variables, renaming variables, if/then logic, arrays, sorting, merging, concatenation, and text manipulation functions. The code contains comments explaining the various techniques.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
92 views34 pages

Sas Sample Syntax

The document shows SAS code for various data manipulation tasks like creating new datasets from existing datasets, modifying and transforming variables, sorting and merging datasets, and applying functions. Key operations demonstrated include selecting/dropping variables, calculating new variables, renaming variables, if/then logic, arrays, sorting, merging, concatenation, and text manipulation functions. The code contains comments explaining the various techniques.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 34

data admit;

set sasuser.admit;
run;

proc print data =admit;


run;

DATA NEW (DROP = ID NAME SEX AGE DATE );


SET ADMIT;

NEW_FEE = 10 * FEE;
BMI = WEIGHT/HEIGHT;
RUN;

PROC PRINT DATA= NEW;


RUN;

/* KEEP STATEMENT */

DATA NEW1 (KEEP = FEE HEIGHT);


SET ADMIT;

RUN;

PROC PRINT DATA= NEW1;

RUN;

/* RENAME */

DATA NEW2;
SET ADMIT (rename=(age=new_age));

myage= new_age*2;

RUN;

PROC PRINT DATA= NEW2;


RUN;

data NEW4 (KEEP = ID NAME HEIGHT) NEW5 (DROP = ID AGE WEIGHT);


set admit;
run;

PROC PRINT DATA= NEW4;


RUN;

PROC PRINT DATA = NEW5;


RUN;

/* _n_ */

data new1(keep=name date) new2(keep=id name age) new3(rename=(id=pt_id));


set admit;
if _n_ =1 then output new1;
if _n_ =2 then output new2;
if _n_ =3 then output new3;
run;

TITLE ' NEW1';


proc print data=new1;
run;
TITLE ' NEW2';
proc print data=new2;
run;
TITLE ' NEW3';
proc print data=new3;
run;

/*gt lt ge le eq*/
/*> < >= <= =*/

/* USING CONDITIONS*/

data NEW1 NEW2 NEW3;

set admit;

IF ACTLEVEL EQ 'LOW' THEN OUTPUT NEW1;


IF ACTLEVEL EQ 'HIGH' THEN OUTPUT NEW2;
IF ACTLEVEL EQ 'MOD' THEN OUTPUT NEW3;

run;

TITLE ' NEW1';


proc print data=new1;
run;
TITLE ' NEW2';
proc print data=new2;
run;

TITLE ' NEW3';


proc print data=new3;
run;

/*WHERE STATEMENT */

data new4(keep=Id name age fee);


set admit;
where age eq 30;
run;

TITLE ' NEW4';

proc print data=new4;


run;

data new4(keep=Id name age fee);


set admit (where=( age gt 40));

run;

TITLE ' NEW4';


proc print data=new4;
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;

proc print data=new7;


run;

data new7;
LENGTH GENDER $ 8;

set admit(keep=Id name age fee gender);


if sex='M' then gender='MALE';
if sex='F' then gender='FEMALE';
run;

proc print data=new7;


run;

data new8(keep=Id name age fee agegrp);


set admit;
if 0 < age < 25 then agegrp='Below 25';
else if 25 <= age <= 30 then agegrp='25 - 30';
else if 30 < age <= 35 then agegrp='30 - 35';
else agegrp='Above 35';
run;

proc print data=new8;


run;

/*null statement */

data _null_;
a='26oct02'd;
put 'SAS date=' a;
put 'formatted date=' a date9.;

run;

SET SASUSER.ADMIT;
RUN;

PROC PRINT DATA = NEW;


RUN;

PROC PRINT DATA = NEW NOOBS;


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;

PROC PRINT DATA = CLASS;


RUN;

DATA cars1;
LENGTH PRICE 8 ;
INPUT make $ model $ mpg weight price;

datalines;
AMC Concord 22 2930 4099
AMC Pacer

17 3350 4749

AMC Spirit 22 2640 3799


Buickhummer Century 20 3250 4816
Buick Electra 15 4080 7827
;
RUN;

proc print data = cars1;


run;

/*FIXED FORMAT DATA */

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;

TITLE "cars222222 data";


PROC PRINT DATA=cars2;
RUN;
TITLE;

DATA cars3;

INPUT @1 make $ 3. @4 model $ 7. @12 mpg @5 STATE$ 2.;


datalines;
AMC Concord 22
AMC Pacer

17

AMC Spirit 22
Bui Century 20
Bui Electra 15
;
RUN;

proc print data = cars3;


run;

DATA NEW2;

INPUT #1 ID 1-3 NAME$ 4-8 SEX$ 9 AGE 10-11


#2 SYSBP 1-3 DIABP 4-5 HT 6-8 WT 9-10;

DATALINES;
101FRANKM27
1208017878
101FRANKM27
1208017878
101FRANKM27
1208017878
101FRANKM27
1208017878
101FRANKM27
1208017878
;
RUN;

PROC PRINT DATA=NEW2;


RUN;

/* READ FROM EXTERNAL FILE */

data cars4;

infile 'C:\Users\xxx\Desktop\new1.txt' DELIMITER =',*'


input car$ model$ mpg;

run;

proc print data= cars4;


run;

data cars4;

infile 'C:\Users\xxx\Desktop\new1.txt' DSD ;


input car$ model$ mpg;

run;

proc print data= cars4;


run;

data date_val;
input date;

informat date date9.;


format DATE WORD20.;

datalines;
01JAN1960
12Apr2011
15jun2012
03mar2010
01JAN1950
;
run;

proc print data=date_val;


run;

data scores;
input name$ 12. +4 score1 $
COMMA8.2;

+6 score2 COMMA11. +1 SALARY

datalines;
Riley

1,132

Henderson

1,015

1,18754

$1000.56

1,10254

$2000.50

RUN;

PROC PRINT DATA = SCORES;


RUN;

DATA NEW1;

INFILE DATALINES firstobs=1 obs = 3 ;

INPUT A1-A6 A7$;

DATALINES;
123456A

123456B
123456C
123456D
;

RUN;

PROC PRINT DATA=NEW1;


RUN;

data weather;

infile datalines missover ;

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;

infile datalines DSD;

length name $5 age 3 city $10;

input name$ age city$;

datalines;
"steve",32,"moanaparis"
;

run;
title ;
proc print data=add noobs ;
run;

01st janauary class 2016

/*YEAR CUTOFF */

proc options option = yearcutoff;


run;

options yearcutoff=1910;

data new1;
date = "01JAN19"d;
put date = date9.;
date1 = "01JAN20"d;
put date1 = date9.;
run;

proc print data = new1;


run;

data new2 ;
set new1;
format date date9.;
format date1 date9.;
run;

proc print data = new2;


run;

/*CONTENTS*/
title;
PROC CONTENTS DATA = new2 ;
RUN;

/*FUNCTIONS */
*hjbfjbfeb;

DATA getdata;

INPUT name $14. x test1 test2 test3;


DATALINES;
John Smith
Samuel Adams
Ben Johnson

4.2 86.5 84.55 81


9.0 70.3 82.37 .
-6.2 82.1 84.81 87

Chris Adraktas

9.5 94.2 92.64 93

John Brown

. 79.7 79.07 72

;
RUN;

proc print data =getdata;


run;

DATA funct1;
SET getdata;
t1int = INT(test1);
t2int = INT(test2);

/* integer part of a number */

t1rnd = ROUND(test1);
t2rnd = ROUND(test2,100); /* round to nearest whole number */
tavg = MEAN(test1, test2, test3);

RUN;

proc print data =funct1;


run;

DATA funct2;

/* mean across variables */

SET getdata;
xsqrt = SQRT(x);

/* square root */

xlog = LOG(x);

/* log base e */

xexp = EXP(x);

/* e raised to the power */

NAME1 = UPCASE (NAME);


NAME2 = LOWCASE (NAME);
NAME3 = PROPCASE (NAME);
LENGTH1 = LENGTH (NAME) ;
T1 = TODAY ();
FORMAT T1 mmddyy10.;

RUN;

PROC PRINT DATA=funct2;

RUN;

DATA NEW1;

INFILE DATALINES;

INPUT A1-A6 ;

DATALINES;
123456
123456
123456

123456
;

RUN;

PROC PRINT DATA=NEW1;


RUN;

DATA NEW2;
SET NEW1;
X1 = MAX (OF A1-A6);
X2 = MIN (OF A4-A6);
RUN;

PROC PRINT DATA=NEW2;


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;

options ps=55 ls=100 nocenter nodate number pageno=1;

DATA ADMIT;
SET SASUSER.ADMIT;
RUN;
TITLE;
proc print data =admit;
run;

/* SORTING DATASET */

data new2;
set sasuser.admit;

run;
proc print;run;

PROC SORT DATA =NEW2 OUT =NEW3;


BY sex actlevel ;
RUN;

PROC PRINT DATA =NEW3;


TITLE'NEW3';
RUN;

/* NODUP AND NODUPKEY*/

DATA getdata;
INPUT name $14. x test1 test2 test3;
DATALINES;
John Smith

4.2 86.5 84.55 81

Samuel Adams

9.0 70.3 82.37 .

Samuel Adams

9.0 70.3 82.37 .

Chris Adraktas

9.5 94.2 92.64 93

Samuel Adams

9.0 70.3 82.37 .

;
RUN;

proc print data =getdata;

run;

PROC SORT DATA =getdata OUT =NEW1 NODUP;


by x;
RUN;

PROC PRINT DATA =NEW1;


RUN;

PROC SORT DATA =NEWDATA OUT =NEW1 NODUP;


by city;
RUN;

PROC PRINT DATA =NEW1;


RUN;

PROC SORT DATA =ADMIT OUT =NEW2 NODUPKEY;


by sex actlevel;
RUN;

PROC PRINT DATA =NEW2;


RUN;

2nd jan 2015 class codes ..

data new2;
set sasuser.admit;

run;
proc print data = new2
;run;

PROC SORT DATA =NEW2 OUT =NEW3;


BY

actlevel sex;

RUN;

PROC PRINT DATA =NEW3;


TITLE'NEW3';
RUN;

/*first. last. retain*/

proc sort data =sasuser.admit out =new4;


by sex ;

run;

proc print data =new4;


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;

proc print data =new5;


run;

proc sort data =sasuser.admit out =new4;


by sex ;

run;

proc print data =new4;


run;

data new5;
set new4;
by sex ;
if first.sex then flag1='A';
if last.sex then flag2='B';

/* if last.sex then wt=1; */


new_var=sum(height,weight);

run;

proc print data =new5;


run;

data new6 (drop = new_var);


set new5;
by sex;
retain new_fee;
if first.sex then new_fee=fee;
run;
proc print;run;

/* SUM FUNCTION */

data newq;
input a b c;
datalines;
123
456
8.5
12.
456
895
;
run;

proc print data=newq;


run;

data m;
set newq;
new=sum( a,b,c);
new1=a+ b + c;

if a ne . and b ne . and c ne . then new1= a+b+c;


run;

proc print data=m;


run;

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;

proc print data = lb;


run;

proc sort data=lb out=lb1;


by subject test visit;
run;

proc print data=lb1;


run;

data lb_1;
set lb1;

by subject test visit;

if first.subject then output;

run;

proc print data=lb_1;


run;

/* LEFT RIGHT AND TRIM */

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*/

data new(drop=first middle last);


first=" Mary ";
middle=" Ann ";
last=" Jones ";
full1=first || middle || last;
V1= LENGTHC(FULL1);
full2=trim(first) || trim(middle) || trim(last);
V2= LENGTHC(FULL2);
full3=trim(left(first)) || ' ' || trim(left(middle)) || ' ' || trim(left(last));
V3= LENGTHC(FULL3);
full4= compress(first) || ' ' || compress(middle) || ' ' || compress(last);
V4= LENGTHC(FULL4);
run;
proc print data=new;
run;

data new(drop=first middle last) ;


first=" Mary ";
middle=" Ann ";

last=" Jones ";


full1=cat(first,middle,last);
full2=cats(first,middle,last);
full3=catt(first,middle,last);
full4=catx(' ',first,middle,last);

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;

proc print data=k;


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;

You might also like