SAS Material
SAS Material
library name :
Dataset name :
Library statement:
Syntax;
Dataset statement:
Syntax;
Data output_dataset;
set input_dataset;
Run;
2
DATA TYPES
SAS has 2 types of date types.
EX:54654654.554
1.Data step
Dataa;
set sashelp.class;
Run;
2.proc step
/*proc print*/
procprintdata=sashelp.class;
run;
3
Datalibref.dataset_name;
setlibref.dataset name;
wherestatement;
run;
Dataoutput_dataset_name;
setinput_dsn;
wherestatement;
run;
CONDITIONAL STATEMENTS
data ds;
set sashelp.class;
where age >= 13;
run;
If statement:
Dataa;
set sashelp.class;
Age2=age*5;
if age2>=70;
Run;
4
If - Elsestatement:
Data ad;
set sashelp.class;
if sex="F"thenFull_sex="FEMALE";
elseFull_sex="MALE";
run;
If-Else If -Elsestatement:
Data ss;
set s;
length sex1 $8;
if sex='M'then sex1='MALE’;
elseif sex='F'then sex1='FEMALE';
else sex1='OTHER';
RUN;
Do_Endstatement:
Data dg1;
set sashelp.class;
length gender $8;
if sex='M'then gender='MALE';
if sex='F'then gender='FEMALE';
if sex='M'then country='US';
if sex='F'then country='AU';
run;
Data dg2;
set sashelp.class;
length gender $8 g1 $5;
if sex='M'thendo;
gender='MALE';
country='US';
g1='Boy';
F1='Young';
end;
if sex='F'thendo;
gender='FEMALE';
country='AU';
g1='Girl';
F1='Young';
end;
5
run;
OPERATORS
1. comparison operators :
Data ss;
setsashelp.class;
wheresex = 'M';
run;
Data ss1;
set sashelp.class;
where name in ("John" ,"Mary");
run;
2. logical operators:
1.AND
2.OR
Dataa;
set sashelp.class;
where sex='M' and Age in (11,15);
run;
Data b;
set sashelp.class;
where sex='M' or Age in (11,15);
run;
3. CONTAINS operators:
Datasd;
set sashelp.class;
where name contains "Al";
Run;
Datasds;
set sashelp.class;
where name ? "Al";
Run;
6
4. Between_Andoperator :
Data sdj2;
set sashelp.class;
where age between13 and 15;
run;
Dataasfr;
set sashelp.class;
where sex='M';
where same and age=15;
run;
6. Like operator: ( %, _ )
DATAasd;
set sashelp.class;
where name like 'J%';
run;
DATA asdw;
set sashelp.class;
where name like 'J%e';
run;
DATAasdwr;
set sashelp.class;
where name like '_a%';
run;
DATA aw;
set sashelp.class;
where name like '__i%';
run;
Data z;
input name$ age;
cards;
sdgf 26
dgf .
dhjkh 5
tuygdf .
fhgthf 53
;
Data df;
set z;
where age=.;
run;
OPTIONS :-
Keep option
Data a;
set sashelp.class(keep=name sex age);
8
run;
Drop option
Data a;
set sashelp.class (drop=name sex age);
run;
Rename option
syntax: rename=(old_name=new_name);
Data d (rename=(name=Emp_name));
set sashelp.class;
run;
Data e;
setsashelp.class (firstobs=1obs=15);
run;
Data e12;
setsashelp.class (firstobs=5obs=15);
run;
Data e1;
setsashelp.class (obs=10);
run;
Data e123;
setsashelp.class (firstobs=16);
9
run;
STATEMENTS :-
Keep Statement
Data a;
set sashelp.class;
keep name sex age;
run;
DropStatement
Data a;
set sashelp.class;
drop name sex age;
run;
Rename Statement
Data dd;
set sashelp.class;
rename name=Emp_name1;
run;
Data dd1;
set sashelp.class;
rename name=Emp_name1 sex=gender1;
run;
lengthstatement:
syntax : variable_name=Expression ;
Data b;
set sashelp.class;
Age1=Age;
Age2=age*2;
Age3=25;
run;
Data b1;
length c $8;
set sashelp.class;
c='ABCD';
run;
Datazz;
set sashelp.class;
order=_n_;
run;
Datasdf;
set sashelp.class;
if _n_ in (1579);
run;
Data c;
set dummy sashelp.class;
run;
11
Data s;
set sashelp.class;
label name='Empolyee_name'
sex ='Employee gender category'
age ='employee age in years'
;
run;
Title Statement
ProcPrintData=Sashelp.Class;
Var Age Sex;
Where Age=15;
Format Age Dollar7.2;
Title"This Is TheSashelp.Class Data";
Run;
Footnote Statement
ProcPrintData=Sashelp.Class;
Var Age Sex;
Where Age=15;
Format Age Dollar7.2;
Title"This Is TheSashelp.Class Data";
Footnote" End TheSashelp.Class Data";
Run;
Data a;
input Name$ sex$ Salary HD;
cards;
ABC M 54564 15000
DEF F 52356 14522
GHI M 86245 11052
FSF M 54563 10358
EHFNM F 456 5321
;
12
DATA AA;
SET A;
FORMAT SALARY COMMA10.1;
RUN;
DATA AAA;
SET A;
FORMAT SALARY COMMA10.1;
RUN;
DATA B;
SET A; FORMAT SALARY DOLLAR10.;RUN;
DATE9. = 01OCT2001
DATE7. = 01OCT01
DATE11.= 01-OCT-2001
DDMMYY8. = 01/10/01
DDMMYY10. = 01/10/2001
YYMMDD10. =2001-10-01
/*8601 FORMAT */
E8601DA. =2001-10-26
E8601DT. =1960-01-01T04:10:00
Attribstatement
Data customers;
inputcust_idcust_name$ acct_open_date;
attribacct_open_dateinformat=date9.format=date9.;
datalines;
111 anand 30apr2016
333 Sanjay 12jan1998
222 srikant 20mar2015
;
run;
13
data s;
set sashelp.class;
if sex='M'thendelete;
if age=14thendelete;
run;
data s1;
set sashelp.class;
if sex='M'thenoutput;
run;
Data s;
input id sex$;
CARDS;
101 M
102 M
103 F
104 F
105 M
106 m
107 N
;
data customers;
inputcust_idcust_name$;
datalines;
111 anand
333 Sanjay
222 srikanth
;
run;
data transactions;
inputcust_idtrans_amount;
datalines;
222 25000
111 30000
333 45000
;
run;
Data a;
set customers transactions;
run;
procsortdata=customers out=c;
by cust_id;
run;
procsortdata=transactions out=t;
by cust_id;
run;
Data transact;
inputcust_idtrans_datetrans_amount;
attribtrans_dateinformat=date9.format=date9.;
datalines;
222 01jun2016 25000
111 05jun2016 30000
333 07jun2016 45000
333 25jun2016 23000
111 15jun2016 27000
;
run;
Datamrg;
merge c1 t1;
by cust_id;
run;
dataset_1------->3variables ;
Result: 5 variables;
dataset_2------->3 variables;
;
16
PROCEDURES
i. proc print
procprintdata=i/p_dsn;
run;
proccontentsdata=i/p_dsn;
run;
data a;
input name$ sex$ age;
cards;
a m 8
b m 9
h f 10
d f 8
g m 8
z m 5
x m 56
h m 1
g m 8
;
procsortdata=a out=b;
by name;
run;
/* syntax*/
procsortdata=i/p_dsnout=o/p_dsnnodupnodupkeydupout=dsn;
by (descending) variable;
run;
procsortdata=a out=bb;
bydescending name;
run;
procsortdata=a out=bbbnodupkey;
bydescending name;
run;
procsortdata=a out=bbbbnodupkeydupout=dsn;
bydescending name;
run;
DATA s1;
set sashelp.class;
length sex1 $8;
if sex='M'then sex1='Male';
else sex1='Female';
run;
DATA s;
length sex $8;
set sashelp.class;
if sex='M'then sex='Male';
else sex='Female';
run;
procformat;
value $ ct "M"="MALE"
"F"="FEMALE"
;
run;
data ss;
set sashelp.class;
format sex $ ct.;
run;
procformat;
18
valuenc_ 11-12=1000
13-14=2000
15-16=3000;
value $ ct_ "M"="MALE"
"F"="FEMALE"
;
run;
procformat;
value _n 11-12=1000
13-14=2000
15-16=3000;
value $ _c "M"="MALE"
"F"="FEMALE"
;
run;
procformat;
valuenc11-12=1000
13-14=2000
15-16=3000
;
run;
datasss;
set sashelp.class;
format age nc.;
run;
procformat;
value nr low-12=1000
13-14=2000
15-high=3000
;
run;
data sss21;
set sashelp.class;
format age nr.;
run;
v. PROC FREQ
19
procfreqdata=i/p;
tables var1;
run;
procfreqdata=sashelp.class;
tables sex;
run;
procfreqdata=sashelp.class;
tables sex/NOCUMNOPERCENT;
run;
procfreqdata=sashelp.class NLEVELS;
tables sex/NOCUMNOPERCENT;
run;
procfreqdata=sashelp.class order=freq;
tables sex/NOCUMNOPERCENT;
run;
Dataasd;
input num;
cards;
5
65
.
98
.
5
;
procfreqdata=asd;
tables num;
run
procfreqdata=asd;
tables num/missing;
run;
20
************************************************************;
procmeansdata=sashelp.class;
Var age;
Run;
procmeansdata=sashelp.classmaxdec=0;
Var age;
Run;
procmeansdata=sashelp.classmaxdec=1;
Var age;
Run;
/*class stmt*/
procmeansdata=sashelp.classmaxdec=0;
Var age;
class sex;
Run;
procmeansdata=sashelp.class summeanminmaxstdnrange ;
var age;
run;
procmeansdata=sashelp.class summeanminmaxstdnrangenoprint;
var age;
outputout=jshsum=s1
mean=mn1
min=mi1
max=ma1
std=sd1
n=n1
range=r1;
21
run;
procmeansdata=sashelp.class summeanminmaxstdnrangenoprint;
var age;
outputout=jsh2
sum=mean=min=max=std=n=range=/autonameautolabel;
run;
Data asd1;
input num;
cards;
5
65
.
98
.
5
;
procfreqdata=asd1;
tables num;
run;
procmeansdata=asd1;
var num;
run;
procmeansdata=asd1 nmiss;
var num;
run;
/*SYNTAX*/
proctransposedata=i/p_dsnout=o/p_dsnname= prefix=
Suffix= ;
varstatement;
idstatement;
bystatement;
Run;
/*Example*/
Datasdf;
inputcust$ account$ balance dollar10.2;
format balance dollar10.2;
cards;
procsortdata=sdfout=sf;
bycust;
run;
proctransposedata=sf out=ff;
id account;
bycust;
var balance;
run;
procreportdata=sashelp.cars;
run;
procreportdata=sashelp.cars ;
column make type model invoice cylinders;
where make='Chevrolet';
run;
procreportdata=sashelp.carsnowdout=dc;
column make type model invoice cylinders;
where make='Chevrolet';
run;
procreportdata=sashelp.carswd;
column make type model invoice cylinders;
where make='Chevrolet';
run;
procreportdata=sashelp.carsnowd;
24
procreportdata=sashelp.carsnowd;
columns ('Chevrolet Vehicles '('First three columns' make
type model)
('last two columns' invoice cylinders)) ;
where make='Chevrolet';
Run;
/*Define stmt*/
procreportdata=sashelp.carsnowd;
column make invoice;
where make='Chevrolet' ;
define make / display ;
define invoice / analysissum ;
run;
procreportdata=sashelp.carsnowd;
column make invoice;
where make='Chevrolet' ;
define make / display'MAKE';
define invoice / analysissum'Sum'f=dollar10.2;
run;
/*ORDER*/
procreportdata=sashelp.carsnowd;
column make type invoice;
define make/order ;
define type / display ;
define invoice / analysissum'Sum'f=dollar10.2;
run;
procreportdata=sashelp.carsnowd;
column make type invoice;
define make/order 'MAKE' ;
define type / display ;
define invoice / analysissum'Sum'f=dollar10.2;
run;
procreportdata=sashelp.carsnowd;
column make type invoice;
define make/order 'MAKE' ;
25
procreportdata=sashelp.carsnowd;
column make type invoice;
define make/order 'MAKE' ;
define type / order descending;
define invoice / analysissum'Sum'f=dollar10.2;
run;
procreportdata=sashelp.carsnowd;
column make type invoice;
define make/order descending'MAKE' ;
define type / order descending;
define invoice / analysissum'Sum'f=dollar10.2;
run;
/* GROUP */
procreportdata=sashelp.carsnowd;
column origin type invoice;
run;
procreportdata=sashelp.carsnowd;
column origin type invoice;
define origin/displaygroup;
define type /display;
define invoice / analysissum;
run;
procreportdata=sashelp.carsnowd;
column origin type invoice;
define origin/displaygroup;
define type /displaygroup;
define invoice / analysissum;
run;
procreportdata=sashelp.carsnowd;
column origin type invoice;
define origin/displaygroup;
26
X. PROC COMPARE
SYNTAX :
FUNCTIONS
Functions are applied to a single variable or to a set of
variables for analyzing and processing data. There are hundreds
of built-in functions in SAS, we will be looking at the most
frequently used and important ones.
CHARACTER FUNCTIONS
Data output_dataset;
set input_dataset;
l=lowcase(variable_name);
u=upcase(variable_name);
p=propcase(variable_name);
Run;
Data a;
set sashelp.class;
l=lowcase(name);
u=upcase(name);
p=propcase(name);
27
Run;
2. SCAN FUNCTION:-
syntax: scan(variable_name,position_number,'delimeters');
Data z;
input name$:25.;
cards;
Ramu.k
Arjun.n
;
Data z1;
set z;
ini=scan(name,1);
f_n=scan(name,2);
Run;
Data y;
input name$:50.;
cards;
[email protected]
[email protected]
;
Data y1;
set y;
f_n=scan(name,1,'.@');
s_n=scan(name,2,'.@');
t_n=scan(name,3,'.@');
Run;
3. SUBSTR FUNCTION
28
syntax:
substr(variable_name,starting_position_number,ending_positio
n_number);
Data q;
input name$:25.;
cards;
101HR
102FINANCE
103ADMIN
;
DATA q1;
set q;
x=substr(name,1,3);
y=substr(name,4,7);
Run;
Data q;
input name$:25.;
cards;
HR101
FINANCE102
ADMIN103
;
DATA q12;
set q;
x=substr(name,1,length(name)-3);
y=substr(name,length(name)-2);
Run;
4.CONCATENATION
29
i.CAT
Data a;
length a b $5;
A='ABCD';
B='EFGH';
c=cat(a,b);
Run;
ii.CATS
Data a1;
length a b $5;
A='ABCD';
B='EFGH';
c=cats(a,b);
Run;
iii.CATX
You can specify the what is the delimeter between two
variables.
Data a12;
length a b $5;
A='ABCD';
B='EFGH';
c=catx('-',a,b);
Run;
5. COMPRESS
Data c;
s="study SAS blog! 7533.";
s1=compress(s,' ');
s2=compress(s,' ','d');
s3=compress(s,' ','kd');
s4=compress(s,' ','a');
s5=compress(s,' ','ka');
s6=compress(s,' ','l');
s7=compress(s,' ','kl');
s8=compress(s,' ','u');
s9=compress(s,' ','ku');
s10=compress(s,' ','p');
s11=compress(s,' ','kp');
s12=compress(s,'SAS','k');
s13=compress(s,'SAS','i');
Run;
6.COMPBL
Syntax : COMPBL(variable_name);
Data h;
z="rajesh kumar";
z1=compbl(z);
run;
7. TRANSLATE
Data d;
length x $6;
x='Gouy';
y=TRANSLATE(x,'pi','uy');
run;
8. TRANWRD
31
Data e;
x=sai sri rwe';
y=TRANWRD(x,'rwe',srinivas);
run;
9. INDEX
10.STRIP
Data h;
D=" DFFGG ";
z2="("||strip(D)||",";
run;
11. TRIM
Data h;
D=" DFFGG ";
z2="("||trim(D)||",";
run;
CONVERSION FUNCTIONS
i. INPUT
ii. PUT
Data hr;
d='01oct1991'd;
s=put(d,date9.);
r=input(s,date9.);
h=put(input(s,date9.),e8601da.);
format d r date9.;
Run;
NUMERIC FUNCTIONS
Data s;
input m1 m2 m3;
a=sum(m1, m2, m3);
a1=sum(of m1-m3);
mi=MIN(m1, m2, m3);
ma=Max(m1, m2, m3);
d=mean(of m1-m3);
e=RANGE(of m1-m3);
l=largest(1, m1,m2,m3);
s=smallest(1, m1,m2,m3);
l2=largest(2 , m1,m2,m3);
s2=smallest(2, m1,m2,m3);
cards;
12 85 65
65 48 23
98 23 45
;
Data ss;
input m1 ;
a=int( m1);
b=round(m1);
c=ceil(m1);
d=FLOOR(m1);
cards;
12.65
51.58
22.000000001
;
DATE FUNCTIONS
TODAY
DAY
MONTH
YEAR
mdy
TIMEPART & DATEPART
INTCK
INTNX
Data a;
s=today();
d=day(s);
m=month(s);
f=year(s);
ss=mdy(m,d,f);
format s ss date9.;
run;
Data a1;
s=today();
dt=datepart(s);
tp=timepart(s);
format s e8601dt. dt date9. tp time10.;
run;
INTERVAL_TYPE: YEAR,QTR,MONTH,WEEK,DAY,HOUR,MINITUS,SEC.
Data ick;
sd='01oct1991'd;
ed='26jan1994'd;
cd=intck('day',sd,ed);
qt=intck('qtr',sd,ed);
yr=intck('year', sd,ed);
formatsd ed date9.;
Run;
SYNTAX: INTNX('INTERVAL_TYPE',
DATE_VALUE,INCREMENT/DECREMENT_NUMBER, 'ALLIGNMENT');
INTERVAL_TYPE: YEAR,QTR,MONTH,WEEK,DAY,HOUR,MINITUS,SEC.
ALLIGNMENTS:B-->BEGIN_VALUE
E--> END_VALUE
M-->MIDDLE_VALUE
S-->SAME_VALUE
;
data as;
td=today();
xyd=intnx('month',today(),-5);
put td=/ yd=/ xyd=;
format td yd xyddate9.;
run;
Datab;
xtd=intnx('day',today(),0);
xyd=intnx('day',today(),-1);
xttd=intnx('day',today(),1);
putxtd=/ xyd=/ xttd=;
formatxtdxydxttddate9.;
run;
Datac;
35
xtd=intnx('month',today(),0);
xyd=intnx('month',today(),-1);
xttd=intnx('month',today(),1);
putxtd=/ xyd=/ xttd=;
formatxtdxydxttddate9.;
run;
Datad;
xtd=intnx('month',today(),0,'e');
xyd=intnx('month',today(),-1,'e');
xttd=intnx('month',today(),1,'e');
putxtd=/ xyd=/ xttd=;
formatxtdxydxttddate9.;
run;
Datae;
xtd=intnx('month',today(),0,'s');
xyd=intnx('month',today(),-1,'s');
xttd=intnx('month',today(),1,'s');
putxtd=/ xyd=/ xttd=;
formatxtdxydxttddate9.;
run;
Dataf;
xtd=intnx('month',today(),0,'m');
xyd=intnx('month',today(),-1,'m');
xttd=intnx('month',today(),1,'m');
putxtd=/ xyd=/ xttd=;
formatxtdxydxttddate9.;
run;
datag;
yr=intnx('year',today(),0);
lyr=intnx('year',today(),-1,'m');
xx=intnx('month',lyr,+1,'m');
dy=intnx('day',xx,-1);
putyr=/ lyr=/ xx=/ dy=;
formatyrlyr xx dydate9.;
run;
ADVANCE SAS
1. Proc sql was introduced in 1990.
36
CREATE A TABLES
PROCSQL;
createtable sri as
select Sex, SUM(AGE) as TOTAL, count (*) ascnt
from sashelp.class
where age between 11and15
groupby sex;
quit;
PROC SQL
procsqlnumber;
select *
from sashelp.class
where age in (13,15)
orderby age;
quit;
SQL PROCEDURE
Procsql (options);
Statements;
quit;
Procsql;
select
from
where
groupby
having
orderby;
quit;
specific variables in dataset
procsql;
37
procsql;
select Name, sex, age, age*2
from sashelp.class;
quit;
procsql;
select Name, sex, age, age*2asDouble_age
from sashelp.class;
quit;
procsql;
select name, sex, catx('_', name, sex) as combine, year(today())
asThis_yr,
month(today()) asmnth, day(today()) asdy
from sashelp.class;
quit;
Where clause
procsql;
select *
from sashelp.class
where age=15;
quit;
Data as1;
set sashelp.class;
where age=15;
run;
PROCSQL;
select Sex, count (*) ascnt
from sashelp.class
where age between 11and15
groupby sex;
quit;
PROCSQL;
select Sex, SUM (AGE) ascnt
38
from sashelp.class
where age between 11and15
groupby sex;
quit;
PROCSQL;
select Sex
from sashelp.class
where age between 11and15
groupby sex;
quit;
PROCSQL;
select Sex, SUM(AGE) as TOTAL, count (*) ascnt
from sashelp.class
where age between 11and15
groupby1;
quit;
Count the number of observations in the dataset
procsql;
select count (*)
from sashelp.class;
quit;
procsql;
select sex, count(*)
from sashelp.class;
quit;
procsql;
select sex, count(*) as ct
from sashelp.class
groupby1;
quit;
HAVING
procsql;
select sex, count(*) as ct
from sashelp.class
groupby1
having ct=11;
quit
order by
39
procsql;
select sex, count(*) as ct
from sashelp.class
groupby1
orderbyctdesc;
quit;
Inobs&outobs
procsqlinobs=3;
select *
from sashelp.class;
quit;
procsqloutobs=3;
select *
from sashelp.class;
quit;
procsqlinobs=3;
select *
from sashelp.class
orderby age desc;
quit;
procsqlinobs=3;
select *
from sashelp.class
where sex='M'
orderby age desc;
quit;
procsqloutobs=3;
select *
from sashelp.class
where sex='M'
orderby age desc;
quit;
40
procsql;
select sex
from sashelp.class;
run;
procsql;
selectdistinct sex
from sashelp.class;
run;
Case Expression
M ----> Age*2;
F ----> Age*3;
Datasss;
set sashelp.class;
if sex="M"thensd=age*2;
elsesd=age*3;
Run;
procsql;
select Name, sex, age,
case
when sex='M'then age*2
when sex='F'then age*3
endassd
from sashelp.class;
quit;
procsql;
select Name, sex, age,
case
when sex='M'then age*2
when sex='F'then age*3
endassd,
case (sex)
when'M'then age*2
when'F'then age*3
endas sd1
from sashelp.class;
quit;
41
Calculated
procsql;
select Name, sex, age,
case
when sex='M'then age*2
when sex='F'then age*3
endassd
from sashelp.class
wheresd>=40;
quit;
procsql;
select Name, sex, age,
case
when sex='M'then age*2
when sex='F'then age*3
endassd
from sashelp.class
where Calculated sd>=40;
quit;
procsql;
select Name, sex, age,
case
when sex='M'then age*2
when sex='F'then age*3
endassd
from sashelp.class
havingsd>=40;
quit;
procsql;
select Name, sex, age,
case
when sex='M'then age*2
when sex='F'then age*3
endassd,
case
when Calculated sd=45then Calculated sd/2
when Calculated sd=42then Calculated sd/3
else Calculated sd/0
endas ds
from sashelp.class;
quit;
42
procsql;
/* create table sri as*/
select name label="Emp_name", Sex label="Emp_sex"
from sashelp.class;
quit;
procsql;
createtable sri1 as
select name label="Emp_name", Sex label="Emp_sex ", Age
label="Emp_age"format=2.
from sashelp.class;
quit;
procsql;
select Name, 'person age', age
from sashelp.class;
quit;
procsql;
select Name, 'person age='||age
from sashelp.class;
quit;
procsql;
select Name, 'person age='||put(age,4.) as Age
from sashelp.class;
quit;
procsql;
select sum(age) as Tot
from sashelp.class;
quit;
43
procsql;
select sum (gc, gcd, gd) asSum_all
fromsashelp.citiqtr;
quit;
procsql;
select round (sum (gc, gcd, gd)) as Sum_all
fromsashelp.citiqtr;
quit;
procsql;
select sex, sum(age) asTot_age
from sashelp.class;
quit;
procsql;
select sex, sum(age) asTot_age
from sashelp.class
groupby1;
quit;
procsql;
select sum(age) asTot_age
from sashelp.class
groupby sex;
quit;
procsql;
select sex, name, sum(age) asTot_age
from sashelp.class
groupby sex, name;
quit;
44
/*COUNT FUNCUNCTION*/
Datadsr;
input num;
cards;
1
2
3
.
.
3
3
.
;
procsql;
select count (*) fromdsr;
quit;
procsql;
select count(num) fromdsr;
quit;
procsql;
select count (distinct num) fromdsr;
quit;
procsql;
select count (*) asTotal_Vars, count(num) asNOt_miss, count
(distinct num) as dist
fromdsr;
quit;
procsql;
select count (*) fromsashelp.citiday;
quit;
procsql;
select count (*), count (SNYDJCM), count (distinct SNYDJCM)
fromsashelp.citiday;
quit;
45
procsql;
select sex, count (*) as tot from sashelp.class
groupby sex;
quit;
data emp;
inputempnoename$ job:$15. mgr hiredate:date9. sal comm deptno;
FORMAT HIREDATE DATE9.;
cards;
7839 KING PRESIDENT . 17NOV1981 5200 . 10
7698 BLAKE MANAGER 7839 01MAY1981 3050 . 30
7782 CLARK MANAGER 7839 09JUN1981 2650 . 10
7566 JONES MANAGER 7839 02APR1981 3175 . 20
7654 MARTIN SALESMAN 7698 28SEP1981 1450 1400 30
7499 ALLEN SALESMAN 7698 20FEB1981 1800 300 30
7844 TURNER SALESMAN 7698 08SEP1981 1700 0 30
7900 JAMES CLERK 7698 03DEC1981 1150 . 30
7521 WARD SALESMAN 7698 22FEB1981 1450 500 30
7902 FORD ANALYST 7566 03DEC1981 3300 . 20
7369 SMITH CLERK 7902 17DEC1980 1000 . 20
7788 SCOTT ANALYST 7566 09DEC1982 5800 . 20
7876 ADAM CLERK 7788 12JAN1983 1300 . 20
7934 MILLER CLERK 7782 23JAN1982 2000 . 10
;
RUN;
DATA DEPT;
INPUT DEPTNO DNAME:$15. LOC&$;
CARDS;
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
;
RUN;
46
procsql;
selectsalfrom emp whereempno=7566;
quit;
procsql;
select *
from emp
wheresal< (selectsalfrom emp whereempno=7566);
quit;
*Read the employees details whose job title is same as FORD's job
title;
procsql;
select job from emp whereupcase(ename)='FORD';
quit;
procsql;
select *
from emp
where JOB = (select job from emp whereupcase(ename)='FORD');
quit;
procsql;
select *
from emp
wherehiredate> (selecthiredatefrom emp whereename='MILLER');
quit;
procsql;
select *
from emp
where deptno = (select deptno from dept wheredname='SALES');
quit;
47
/*Read the clerk employees details who are working in "new york"
location*/
procsql;
select deptno from dept where loc='NEW YORK';
quit;
procsql;
select *
from emp
where deptno = (select deptno from dept where loc='NEW YORK')
and job='CLERK';
quit;
procsql;
select *
from emp
wheredeptno= (selectdeptnofrom emp whereename='FORD');
quit;
procsql;
select job from emp
where deptno = (select deptno from dept wheredname='SALES');
quit;
procsql;
select *
from emp
where deptno=20 and job IN ('MANAGER','CLERK');
quit;
/*Final Query*/
procsql;
select *
from emp
wheredeptno= (selectdeptnofrom emp whereename='FORD')
and job IN (select job from emp
where deptno = (select deptno from dept wheredname='SALES'));
quit;
procsql;
select max(sal) from emp;
quit;
procsql;
select * from emp where sal=5800;
quit;
/* Final Query*/
procsql;
select * from emp where sal=(select max(sal) from emp);
quit;
/*Display the names of all employees who earns more than the
average salary of the company? */
procsql;
select avg(sal) from emp;
quit;
procsql;
select * from emp where sal > (select avg(sal) from emp);
quit;
procsql;
select deptno, min(sal) as min_sal
from emp
groupby deptno;
quit;
procsql;
select min(sal)
from emp
where deptno=20;
quit;
/* Final Query*/
procsql;
49
procsql;
select max(sal)
from emp
groupby deptno;
quit;
procsql;
select *
from emp
where sal IN (5200,5800 ,3050);
quit;
procsql;
select *
from emp
where sal IN (select max(sal)
from emp
groupby deptno);
quit;
procsql;
select *
from emp
where sal IN (select max(sal)
from emp
groupby job);
quit;
/*
Find the Employee details who are receiving less salary than the
salary received by any SALESMAN?
50
*/
procsql;
select sal
from emp
where job='SALESMAN';
quit;
procsql;
select *
from emp
where sal <ANY (select sal from emp where job='SALESMAN');
quit;
procsql;
select *
from emp
where sal < (select MAX (sal) from emp where job='SALESMAN');
quit;
/*
Find the Employee details who are receiving more salary than the
salary received by any SALESMAN?
*/
procsql;
select *
from emp
where sal >ANY (select sal from emp where job='SALESMAN');
quit;
procsql;
select *
from emp
where sal > (select MIN (sal) from emp where job='SALESMAN');
quit;
/*
Find the Employee details who are receiving salary greater
than the salary received by deptno=30?
51
*/
procsql;
select sal
from emp
where deptno=30;
quit;
procsql;
select *
from emp
where sal >ALL (select sal from emp where deptno=30);
quit;
procsql;
select *
from emp
where sal > (select max(sal) from emp where deptno=30);
quit;
procsql;
select * from emp
where sal>ALL (select avg(sal) from emp groupby deptno);
quit;
procsql;
select * from emp
where sal>select max (A.avg_sal)
from (select avg(sal) as avg_sal from emp groupby deptno) A;
quit;
/* Find the Employee details who are receiving salary lesser than
the Average salary received by each deptno? */
procsql;
select * from emp
where sal<ALL (select avg(sal) from emp groupby deptno);
quit;
JOINS
52
data debit_card_customers;
input cust_id debit_acct_number name$ dc_open_date dc_balance;
informat dc_open_date date9.;
format dc_open_date date9.;
datalines;
111 123456 anand 12jun2000 1000
222 234567 srikant 20sep2013 2000
444 456789 prathap 20mar2012 5000
333 345678 vishwa 29aug2013 7500
;
run;
data credit_card_customers;
input cust_id credit_acct_number cc_open_date cc_balance;
informat cc_open_date date9.;
format cc_open_date date9.;
datalines;
444 444444 01jul2016 27000
555 555555 20jun2016 33000
111 111111 01jun2015 23450
;
run;
/*Cartesian product*/
procsql;
select *
from debit_card_customers ,credit_card_customers;
quit;
/*inner join*/
procsql;
select *
from debit_card_customers innerjoin credit_card_customers
on debit_card_customers.cust_id=credit_card_customers.cust_id;
quit;
procsql;
select debit_card_customers.cust_id,
debit_card_customers.debit_acct_number,
credit_card_customers.credit_acct_number
53
procsql;
selectdc.cust_id,
dc.debit_acct_number,
cc.credit_acct_number
from debit_card_customers dc innerjoin credit_card_customers cc
on dc.cust_id = cc.cust_id;
quit;
procsql;
selectdc.cust_id,
dc.debit_acct_number,
cc.credit_acct_number
from debit_card_customers as dc innerjoin credit_card_customers
as cc
on dc.cust_id = cc.cust_id;
quit;
procsql;
selectdc.cust_id,
debit_acct_number,
credit_acct_number
from debit_card_customers as dc innerjoin credit_card_customers
as cc
on dc.cust_id = cc.cust_id;
quit;
procsql;
select *
fromadam.adslinnerjoin adam.adae
on adam.adsl.USUBJID = adam.adae.USUBJID;
quit;
procsqlnumber;
selectad.USUBJID , ae.AETERM
fromadam.adsl ad inner join adam.adae ae
on ad.USUBJID = ae.USUBJID;
quit;
procsqlnumber;
54
procsqlnumber;
selectad.USUBJID , ae.AETERM, count (*)
fromadam.adsl ad innerjoin adam.adae ae
on ad.USUBJID = ae.USUBJID
groupby1,2;
quit;
procsqlnumber;
selectad.USUBJID , ae.AETERM, count(*) as cn
fromadam.adsl ad innerjoin adam.adae ae
on ad.USUBJID = ae.USUBJID
groupby1,2
having cn>1;
quit;
procsql;
select column-list
from Table1 innerjoin table2
on join-condition
<other clauses>;
quit;
procsql;
select column-list
from Table1, table2
wherejoin-condition
<other clauses>;
quit;
procsqlnumber;
selectad.USUBJID , ae.AETERM
fromadam.adsl ad , adam.adae ae
wheread.USUBJID = ae.USUBJID;
quit;
55
175 -acct_num
100 - acct_num
50 - comm
procsql;
selectdc.cust_id, cc.cust_id, debit_acct_number,
credit_acct_number
from debit_card_customers dc fulljoin credit_card_customers cc
on dc.cust_id=cc.cust_id;
quit;
procsql;
select coalesce (dc.cust_id, cc.cust_id) as cust_id,
debit_acct_number, credit_acct_number
from debit_card_customers dc fulljoin credit_card_customers cc
on dc.cust_id=cc.cust_id;
quit;
Data AB;
MERGE A(IN=var1) B(IN=var2);
by common-variable;
if var1 =1 and var2=1;
run;
/* Example */
procsql;
selectdc.*,cc.*
from debit_card_customers dc innerjoin credit_card_customers cc
on dc.cust_id=cc.cust_id;
quit;
56
data dc_cc;
merge debit(in=x) credit(in=y);
by cust_id;
if x=1 and y=1;
run;
procprintdata=dc_cc;
run;
/* Left Join */
/* Syntax */
procsql;
select colum-list
from TableA leftjoin TableB
on Join-condition
<other clauses>;
quit;
Data AB;
MERGE A(IN=var1) B(IN=var2);
by common-variable;
if var1 =1;
run;
/* Example */
procsql;
selectdc.*,cc.*
from debit_card_customers dc leftjoin credit_card_customers cc
on dc.cust_id=cc.cust_id;
quit;
procsortdata=debit_card_customers out=debit; by cust_id; run;
procsortdata=credit_card_customers out=credit; by cust_id; run;
data dc_cc;
merge debit(in=x) credit(in=y);
by cust_id;
if x=1;
run;
57
procprintdata=dc_cc;
run;
data dc_cc;
merge debit(in=x) credit(in=y);
by cust_id;
if x=1 and y=0;
run;
procprintdata=dc_cc;
run;
/* Syntax */
procsql;
select colum-list
from TableA rightjoin TableB
on Join-condition
<other clauses>;
quit;
Data AB;
MERGE A(IN=var1) B(IN=var2);
by common-variable;
if var2 =1;
run;
/* Examples */
procsql;
selectdc.cust_id, debit_acct_number,
name,cc.credit_acct_number, cc_open_date
from debit_card_customers dc rightjoin credit_card_customers cc
on dc.cust_id=cc.cust_id;
quit;
procsql;
58
selectdc.*,cc.*
from debit_card_customers dc rightjoin credit_card_customers cc
on dc.cust_id=cc.cust_id;
quit;
data dc_cc;
merge debit(in=x) credit(in=y);
by cust_id;
if y=1;
run;
procprintdata=dc_cc;
run;
procsql;
selectcc.*
from debit_card_customers dc rightjoin credit_card_customers cc
on dc.cust_id=cc.cust_id
wheredc.cust_id is null;
quit;
data dc_cc;
merge debit(in=x) credit(in=y);
by cust_id;
if x=0 and y=1;
run;
procprintdata=dc_cc;
run;
/* Full Join */
/* Syntax */
procsql;
select colum-list
from TableA fulljoin TableB
59
on Join-condition
<other clauses>;
quit;
Data AB;
MERGE A B;
by common-variable;
run;
/* Example */
procsql;
select coalesce (dc.cust_id, cc.cust_id) as cust_id,
debit_acct_number, name, dc_open_date, dc_balance
, cc.credit_acct_number, cc_open_date, cc_balance
data dc_cc;
merge debit credit;
by cust_id;
run;
procprintdata=dc_cc;
run;
procsql;
select coalesce (dc.cust_id, cc.cust_id) as cust_id,
debit_acct_number, name, dc_open_date, dc_balance
, cc.credit_acct_number, cc_open_date, cc_balance
data dc_cc;
merge debit(in=x) credit(in=y);
60
by cust_id;
if (x=1 and y=0) or (x=0 and y=1);
run;
procprintdata=dc_cc;
run;
SET OPERATORS
Combining tables vertically,is nothing but concatenation in
datastep.
1.UNION
2.OUTER UNION
3.EXCEPT
4.INTERSECT
data A;
input num;
datalines;
1
2
;
data B;
input num;
cards;
6
4
5
2
;
/*union*/
procsql;
select * from A
union
select * from B;
quit;
procsql;
select * from A
unionall
select * from B;
61
quit;
/* outer union*/
procsql;
select * from A
outerunion
select * from B;
quit;
/*EXCEPT*/
procsql;
select * from A
EXCEPT
select * from B;
quit;
procsql;
select * from B
EXCEPT
select * from A;
quit;
/*INTERSECT*/
procsql;
select * from A
INTERSECT
select * from B;
quit;
*****************************************;
data AA;
input name$ gender$;
datalines;
praveen M
Srinu M
;
data BB;
input gender$ name$;
cards;
F Bharathi
M Ravi
M Mahi
62
procsql;
select * from AA
union
select * from BB;
quit;
procsql;
select name, gender from AA
union
select name, gender from BB;
quit;
procsql;
select * from AA
unioncorr
select * from BB;
quit;
data A;
input num;
datalines;
1
2
1
;
data B;
input num;
cards;
6
4
5
2
;
procsql;
select * from A
INTERSECT
select * from B;
quit;
63
procsql;
select * from A
except
select * from B;
quit;
procsql;
select * from A
exceptall
select * from B;
quit;
procsql;
(select num from A
except
select num from B)
union
(select num from B
except
select num from A)
;
quit;
/* MODIFIER*/
MACROSS
procprintdata=sashelp.class;
footnote" Result for 18/03/2019";
run;
procprintdata=sashelp.class;
footnote" Result for &sysdate9.";
run;
64
%let gg =M;
data dsd;
set sashelp.class;
where sex="&gg";
title"Data for &gg";
run;
procprintdata=dsd;
footnote"Result for &gg";
run;
/*Macro triggers */
Data_null_;
a="srinu";
put a=;
run;
%let Name=Srinivas;
/*%put Statement */
%put&Name;
%put&=Name;
/*referring a macro */
¯o_name
%put _automatic_;
%let Nm=Srdsfs;
%put _user_;
%put _all_;
syntax:
%let name=value;
%let x=10;
%let y=20;
%let z=&x+&y;
%put _user_;
%let z=%eval(&x+&y);
%let n4=;
%let n5=3+4;
%let n6=0;
%let n7=*;
66
%put _user_;
%symdel n2 n3;
ex;
%let x=10;
%put&=x &x;
%put y&x;
%put&x y;
%put&xy;
%put&x.y;
%let lib=sashelp;
%let dtn=class;
procprintdata=&lib..&dtn.;
run;
yy20jan20192001
jan=01;
yy=2019;
%let jan=01
%let yy=2019
%put _user_;
%let jan=01;
%let yy=2019;
67
%let dt=20;
%put&yy&dt&jan;
%put&=sxx;
procprintdata=sashelp.air;
run;
%let macro_name = macro_value;
%put stmt;
********************************;
/*Macro Functions */
/*%substr*/
%put&sysdate9.; 15OCT2014;
Data a;
x=%substr(&sysdate9.,6);
run;
Data abc;
x=10;
y=20;
run;
/*%scan*/
procprintdata=%scan(&syslast.,2);
run;
/*1*/
%let x=10;
%let y=20;
%let z=&x+&y;
%put&=z;
/*2*/
68
%let x=10;
%let y=20;
%let z=%eval(&x+&y);
%put&=z;
/*3*/
/*i*/
%let x1=10.5; /* floating values are not allowed in eval func
*
%let y1=20.5;
%let z1=%eval(&x1+&y1);
%put&=z1;
/*ii*/
%let x1=10.5;
%let y1=20.5;
%let z1=%sysevalf(&x1+&y1);
%put&=z1;
/*iii*/
%let x1=10;
%let y1=20;
%let z1=%sysevalf(&x1+&y1);
%put&=z1;
/*4*/
/*i*/
69
%let x1=2;
%let y1=1;
%let z1=%eval(&x1/&y1);
%put&=z1;
/*ii*/
%let x1=2;
%let y1=1;
%let z1=%eval(&y1/&x1);
%put&=z1;
/*iii*/
%let x1=2;
%let y1=1;
%let z1=%sysevalf(&y1/&x1);
%put&=z1;
/*%Sysfunc function */
%put&=statement;
%put&=statement;
%put&=statement;
%put&=statement;
%put&=statement;
***************Define Macro****************;
%macromacro_name;
data step;
proc step;
proc sql;
global stmts;
macro stmts;
%mend macro_name;
/* Call a macro*/
71
%macro_name
/*i*/
%macrotest;
%let x=11;
%put&=x;
%mend;
%test
%put&=x;
/*ii*/
%let x=15;
%macrotest;
%let x=11;
%put&=x;
%mend;
%test
libname ss "C:\Users\LENOVO\Desktop\mac";
optionsmstoredsasmstore=ss;
%macrotest1/store source;
%let x=11;
%put&=x;
%mend;
%test1
libname ss "C:\Users\LENOVO\Desktop\mac";
optionsmstoredsasmstore=ss;
%test1
********************************;
72
/*macro parameters */
1. positional parameters.
2. keyword parameters.
3. Mixed Parameters.
procprintdata=sashelp.air;
run;
procprintdata=sashelp.class;
run;
procprintdata=sashelp.buy;
run;
%prt (sashelp.air);
%prt (sashelp.class);
%prt (sashelp.buy);
/*positional */
optionsmprint;
%srt (sashelp.class, cls, sex)
/*keyword */
73
optionsmprintsymbolgen;
%srt1 (od=cls2, ip=sashelp.class, var=sex)
/*Mixed */
optionsmprintsymbolgenmlogic;
%srt1 (sashelp.class, od=cls3, var=sex)
*********************************************;
Data_null_;
call symput ("x",100);
run;
%put&=X;
Data_null_;
call symput ("x",100);
call symputx ("y",100);
run;
%put&=X &=y;
Datasd;
set sashelp.class;
run;
Datasd;
set sashelp.class;
callsymputx("Nm", name);
run;
Datasd;
set sashelp.class;
call symputx("Nm"||(compress(_N_)), name);
run;
Data_null_;
set sashelp.class end=cn;
if cn=1thencall symputx("count",_n_);
run;
%put&=count;
procsql;
selectcount(*) into:cnt
from sashelp.class;
quit;
%put&=cnt;
procsql;
selectdistinct sex
75
into:sx
from sashelp.class;
quit;
%put&=sx;
procsql;
selectdistinct sex
into:sx1-:sx2
from sashelp.class;
quit;
%put&=sx1 &=sx2;
procsql;
selectdistinct sex
into:sxx separated by','
from sashelp.class;
quit;
%put&=sxx;
/*Do-loops */
%macrodl;
%do i=1%to10;
%put title " value for &i";
%end;
%mend;
%dl