0% found this document useful (0 votes)
40 views75 pages

SAS Material

The document provides a comprehensive overview of the SAS (Statistical Analysis System) programming language, detailing naming conventions for libraries, datasets, and variables, as well as various data types. It outlines programming steps, conditional statements, operators, and options for managing datasets, including keeping, dropping, and renaming variables. Additionally, it covers procedures for data manipulation, including concatenation and merging of datasets, and formatting options for output.

Uploaded by

akash behera
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views75 pages

SAS Material

The document provides a comprehensive overview of the SAS (Statistical Analysis System) programming language, detailing naming conventions for libraries, datasets, and variables, as well as various data types. It outlines programming steps, conditional statements, operators, and options for managing datasets, including keeping, dropping, and renaming variables. Additionally, it covers procedures for data manipulation, including concatenation and merging of datasets, and formatting options for output.

Uploaded by

akash behera
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 75

1

SAS : STATISTICAL ANALYSIS SYSTEM OR SOFTWARE

library name :

1. library name length is 8 characters or less.


2. library name start with alphabets or ' _ '.
Subsequentvalues are alphabets, numerals,
special symbols.
3.library name is upper case, lower case, mixed case.

Dataset name :

1. dataset name length is 1 to 32 characters.


2. dataset name start with alphabets or ' _ '.
subsequentvalues are alphabets, numerals,
special symbols.
3. dataset name is upper case, lower case, mixed case.

Variable names rules:

1. variable name length is 1 to 32 characters.


2. variable name start with alphabets or ' _ '.
subsequent values are alphabets, numerals,
special symbols.
3. variable name is upper case, lower case, mixed case.

Library statement:

Syntax;

libname library_reference " path ";

Dataset statement:

Syntax;

Data output_dataset;
set input_dataset;
Run;
2

DATA TYPES
SAS has 2 types of date types.

1. Character data type.


 alphabets, special symbols(@,.,,), numerals.
Ex: '34gfddd56'"4567"
2. Numeric data type.
Numerics

EX:54654654.554

Date is also numeric data type.

SAS PROGRAMING STEPS:

SAS program contains one or more steps.


1.Data step
2.proc step

1.Data step

Dataa;
set sashelp.class;
Run;

2.proc step

/*proc print*/

procprintdata=sashelp.class;
run;
3

Every SAS statement end with semicolumn.

Data step: is used to create new data set.


set step: is used to read the input dataset.
where statement: is used to restrict/subset the data.;

Datalibref.dataset_name;
setlibref.dataset name;
wherestatement;
run;

Dataoutput_dataset_name;
setinput_dsn;
wherestatement;
run;

INPUT DATASET CREATION:


Data z;
input name$ age;
cards;
sdgf 26
dgf 23
dhjkh 5
tuygdf 34
fhgthf 53
;

CONDITIONAL STATEMENTS

Where statement: is used to restrict/subset the data.;

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 :

=, >, <, <= ,>= ,IN

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;

5. Where same and operator:

Dataasfr;
set sashelp.class;
where sex='M';
where same and age=15;
run;

6. Like operator: ( %, _ )

%: it will display number of characters.


_: only one character;

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;

Contains, like both are same in below example


7

7. is null & is missing

ifcharacter data is missing in dataset, then data value is


space.
when ever you are executing the missing characters then
variable_name="";

ifnumeric data is missing in dataset, then data value is


period(.).
When ever you are executing the missing numerics then
variable_name=.;

Data z;
input name$ age;
cards;
sdgf 26
dgf .
dhjkh 5
tuygdf .
fhgthf 53
;

Data df;
set z;
where age=.;
run;

DATA SET OPTIONS AND STATEMENTS :-


Keep : keep the variables into dataset.
Drop : Drop the variables into dataset.
Rename: Change the name in the variables.
Assignment statement: To create a new variable using an
assignment statement.

OPTIONS :-

Keep option

Data a;
set sashelp.class(keep=name sex age);
8

run;

Data aa(keep=name sex age);


set sashelp.class;
run;

Drop option

Data a;
set sashelp.class (drop=name sex age);
run;

Data aa(drop=name sex age);


set sashelp.class;
run;

Rename option

syntax: rename=(old_name=new_name);

Data d (rename=(name=Emp_name));
set sashelp.class;
run;

Dataddd (rename=(name=Emp_name sex=gender));


set sashelp.class;
run;

First Obs and Obs

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: length variable_name(s) given_length;

Retain statement:It is used to set variables in order as per


requirement.
Data a;
retain Age weight;
set sashelp.class;
run;
10

AssignmentStatement: To create the new variable in the


dataset

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;

OUTPUT Statement :It is used to create a new observation for


particular variable.
Data dummy;
name="Gopi";output;
run;

Data c;
set dummy sashelp.class;
run;
11

Label statement: It is used to assign the descriptive test in


variables

syntax: labelvariable_name="discriptive Text";

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;

Format statement:Formating the values

syntax: format variable(S)_name format_type;

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

YYMMN6. =200105 --->(YYYYMM)

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

DELETE DATASETS IN LIBRARY


procdatasetslib=work killmemtype=data;
run;quit;

data s;
set sashelp.class;
if sex='M'thendelete;
if age=14thendelete;
run;

data s1;
set sashelp.class;
if sex='M'thenoutput;
run;

CREATE MULTIPLE DATASETS


Datawork.Malework.Female;
set sashelp.class;
if sex='M'thenoutputwork.Male;
elseoutputwork.Female;
run;

Data s;
input id sex$;
CARDS;
101 M
102 M
103 F
104 F
105 M
106 m
107 N
;

Data Male1 Female1 Others;


set s;
if sex='M'thenoutput Male1;
elseif sex='F'thenoutput Female1;
elseoutput Others;
run;
14

COMBINING SAS DATASETS


1.Concatenation.
2.Merging.

1.Concatenation: Combining two or more SASdata sets


vertically.;

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;

2.Merging:Combining two or more SAS data sets horizontally.


Data m;
merge customers transactions;
run;

prereqisites: 1. i/p data sets must have at least one common


variable.
2. i/p data sets sorted based on common variable.
15

procsortdata=customers out=c;
by cust_id;
run;

procsortdata=transactions out=t;
by cust_id;
run;

1.one to one merge.


Data m1;
merge c t;
by cust_id;
run;

2. one to many merge.

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;

procsortdata=customers out=c1; by cust_id; run;

procsortdata=transact out=t1; by cust_id; 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;

procprint procedure is used to print the data portion of the


data set.;

ii. proc contents

proccontentsdata=i/p_dsn;
run;

It is used to descriptor portion of the dataset , nothing


but meta data information of dataset .

iii. PROC SORT PROCEDURE

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;

/* by default, ascending order */


17

procsortdata=a out=bb;
bydescending name;
run;

procsortdata=a out=bbbnodupkey;
bydescending name;
run;

procsortdata=a out=bbbbnodupkeydupout=dsn;
bydescending name;
run;

iv. PROC FORMAT: This is used in datastep, proc step.

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

proc freq: if you want to generate frequency table

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

vi. PROC MEANS:

It is used to create the summary statistics.

************************************************************;

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;

vii. PROC TRANSPOSE


22

The meaning of proc transpose is convert the columns into rows


and rows into columns.

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

smith checking $1,000.00


smith savings $4,000.00
smith mortgage $150,000.00
smith credit_card $500.00
jones checking $973.78
jones savings $2,613.44
jones mortgage .
jones credit_card $140.48
;

procsortdata=sdfout=sf;
bycust;
run;

proctransposedata=sf out=ff;
id account;
bycust;
var balance;
run;

viii. PROC IMPORT AND EXPORT PROCEDURE


23

procimportdatafile="C:\Users\LENOVO\Desktop\Raw Data use


import\Cls.xls"
out=xl
dbms=xlsx
replace;
run;

PROCEXPORTOUTFILE="C:\Users\LENOVO\Desktop\Raw Data use


import\Expt_Xls.xls"
DATA=SASHELP.CLASS
DBMS=xls
replace;
RUN;

ix. PROC REPORT

procreportdata=sashelp.cars;
run;

/*1.Column stmt : Describe the arrangement of all columns


and of headings that span more than one column .
COLUMN STMT SPECIFY THE ORDER OF THE VARIABLES */

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;

/*2.Title for the inside table */

procreportdata=sashelp.carsnowd;
24

columns ('Chevrolet Vehicles ' make type model invoice


cylinders);
where make='Chevrolet';
run;

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

define type / order ;


define invoice / analysissum'Sum'f=dollar10.2;
run;

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

define type /displaygroup;


define invoice / analysismean;
run;

X. PROC COMPARE

 Basic comparison of two datasets.


 Comparing two datasets with a common ID variable.
 Comparing Metadata (dataset attributes) between two
datasets.
 Comparing datasets by Observations.
 Comparing variables within a Dataset.
 Comparing specific values in an output dataset.

SYNTAX :

Proc compare base=dsname compare=dsname;


Run;

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

1. LOWCASE, UPCASE AND PROPCASE:-

It is used to Convert all letters in lowcase, Upcase and


Propcase

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:-

It is used to Convert one variable into multiple variables


based on delimeter.

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

It is used to Convert one variable into multiple


variables

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

It is used to Combining two or more variables intoone


variable.

i.CAT

It will not remove any leading and trailing spaces for


two or more variables.

Syntax: cat(var1, var2);

Data a;
length a b $5;
A='ABCD';
B='EFGH';
c=cat(a,b);
Run;

ii.CATS

It will remove any leading and trailing spaces for two


or more variables.

Syntax: cats(var1, var2);

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

It will remove unwanted characters from a given string.


30

Synax: compress(variable_name,' ','options');

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

It will convert multiple blanks(spaces) into single


blank.

Syntax : COMPBL(variable_name);

Data h;
z="rajesh kumar";
z1=compbl(z);
run;
7. TRANSLATE

It is used to replace the characters into a given string

Syntax : translate(variable_name, 'new_character', '


old_character');

Data d;
length x $6;
x='Gouy';
y=TRANSLATE(x,'pi','uy');
run;

8. TRANWRD
31

It is used to replace the word(s) into a given string

Syntax : tranwrd(variable_name, 'old_word', 'new_word');

Data e;
x=sai sri rwe';
y=TRANWRD(x,'rwe',srinivas);
run;

9. INDEX

Find the position of the given character.

Syntax : Index(variable_name, 'given_character');

10.STRIP

It is used to remove unwanted leading and trailing blanks or


spaces in particular variable.

Data h;
D=" DFFGG ";
z2="("||strip(D)||",";
run;

11. TRIM

It is used to remove unwanted trailing blanks or spaces in


particular variable.

Data h;
D=" DFFGG ";
z2="("||trim(D)||",";

run;

CONVERSION FUNCTIONS

Two Types Of Conversion Functions


32

i. INPUT

It is used convert character type into numeric type.

ii. PUT

It is used convert numeric type into character type.

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

/*SUM MIN MAX MEAN RANGE LARGEST SMALLEST*/;

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
;

/* INT ROUND CEIL FLOOR*/


33

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;

INTCK :CHECK THE NUMBER OF INTERVELS BETWEEN TWO DATE/TIME


VALUES.
34

SYNTAX: INTCK('INTERVAL_TYPE',START_DATE, END_DATE);

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;

INTNX :TO INCREMENT OR DECREMENT THE DATE VALUES.

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

2. proc sql is not a replacement of data step. It’s a quarrying


tool.
3. proc sql is not a custom reporting tool.
4. In proc sql
Datasets are nothing but TABLES
Variables are nothing but COLUMNS
observations are nothing but ROWS

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

select name, age, sex


from sashelp.class;
quit;

create a new variable

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;

Group By Clause: Summarizing the data

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

Remove the duplicates

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

Specific labels & formats

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;

/*Adding character constant*/

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;

/*Additional information of summary func & group by class */

/*1. specify only one column */

procsql;
select sum(age) as Tot
from sashelp.class;
quit;
43

/*2. Specify multiple columns */

procsql;
select sum (gc, gcd, gd) asSum_all
fromsashelp.citiqtr;
quit;

procsql;
select round (sum (gc, gcd, gd)) as Sum_all
fromsashelp.citiqtr;
quit;

/* 3. Use columns and Summary func */

procsql;
select sex, sum(age) asTot_age
from sashelp.class;
quit;

/*4. Use columns, Summary func and group by clause*/

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;

procdatasetslib=work mt=data killnolist;


run; 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

*Read the employee details whose salary is less than the


salary with empno=7566;

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;

/*Read all the employee’s details who joined after 'MILLER'*/


procsql;
selecthiredatefrom emp whereename='MILLER';
quit;

procsql;
select *
from emp
wherehiredate> (selecthiredatefrom emp whereename='MILLER');
quit;

/*Read the sales dept employee details*/


Procsql;
select deptno from dept wheredname='SALES';
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;

/*Read the employee details whose department is same as FORD's


deptno and
job title type should be same as sales dept job titles? */

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;

/*Find the employee details who is receiving highest salary? */


48

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;

/*Display the deptno, minimum salary paid by each department?

condition: whose minimum salary is greater than minimum salary


received by dept=20
*/

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

select deptno, min(sal) as min_sal


from emp
groupby deptno
having min_sal > (select min(sal) from emp where deptno=20);
quit;

/*Find the Employee details who are receiving maximum salary in


each department? */

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;

/*Find the Employee details who are receiving maximum salary in


each Job Category? */

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;

/* Find the Employee details who are receiving salary greater


than the Average salary received by each deptno? */

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

from debit_card_customers innerjoin credit_card_customers


on debit_card_customers.cust_id = credit_card_customers.cust_id;
quit;

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;

libname adam "C:\Users\lenovi\Desktop\SAS\Rubina"INENCODING=ANY;

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

selectad.USUBJID , ae.AETERM, count(*)


fromadam.adsl ad innerjoin adam.adae ae
on ad.USUBJID = ae.USUBJID
groupby1;
quit;

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;

/* Alternate Syntax - Inner join */

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

tableA - debit card


tableB - credit card

175 -acct_num
100 - acct_num
50 - comm

inner join -50


left join- 175
full join - 175+100-50=225;

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;

/* Inner join - Using Datastep Merge*/

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

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 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;

/* Left join - Using Datastep Merge*/

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;

/* Only debit card customers */


procsql;
selectdc.*,cc.*
from debit_card_customers dc leftjoin credit_card_customers cc
on dc.cust_id=cc.cust_id
wherecc.cust_id is null;
quit;

data dc_cc;
merge debit(in=x) credit(in=y);
by cust_id;
if x=1 and y=0;
run;

procprintdata=dc_cc;
run;

/* Right Join - Syntax */

/* Syntax */
procsql;
select colum-list
from TableA rightjoin TableB
on Join-condition
<other clauses>;
quit;

/* Right join - Using Datastep Merge*/

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;

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 y=1;
run;

procprintdata=dc_cc;
run;

/* Only credit card customers */


procsql;
selectdc.*,cc.*
from debit_card_customers dc rightjoin credit_card_customers cc
on dc.cust_id=cc.cust_id
wheredc.cust_id is null;
quit;

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;

/* Full join - Using Datastep Merge*/

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

from debit_card_customers dc fulljoin credit_card_customers cc


on dc.cust_id=cc.cust_id
;
quit;

data dc_cc;
merge debit credit;
by cust_id;
run;

procprintdata=dc_cc;
run;

/* Customers having only one card (either debit/Credit) */

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

from debit_card_customers dc fulljoin credit_card_customers cc


on dc.cust_id=cc.cust_id
wheredc.cust_id is null or cc.cust_id is null
;
quit;

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

ALLMODIFIER : DEFAULT BEHAVIOURS FOR ROWS.


CORR MODIFIER: DEFAULT BEHAVIOURS FOR COLUMNS.

MACROSS

It is used for customizing the generation of sas code, as well as


minimize the
amount of code that u muster enter.

procprintdata=sashelp.class;
footnote" Result for 18/03/2019";
run;

procprintdata=sashelp.class;
footnote" Result for &sysdate9.";
run;
64

/*create a macro variable */

syntax: %let name=value;

%let gg =M;

%let gg1 ="M";

data dsd;
set sashelp.class;
where sex="&gg";
title"Data for &gg";
run;

procprintdata=dsd;
footnote"Result for &gg";
run;

/*Macro triggers */

&----> Referring the macro.

%----> macro stmt, macro func, macro call starts with %.


;

Data_null_;
a="srinu";
put a=;
run;

%let Name=Srinivas;

/*%put Statement */

%put&Name;

%put&=Name;

%put person name is &Name;

/*Types of macro variables */

1. Automatic macro variables


2. User defined macro variables.
65

/*referring a macro */

&macro_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);

%put today date is: &sysdate9.;

%let n1=Ed nortan;

%let n2="Ed nortan";

%let n3="jan's fort";

%let n4=;

%let n5=3+4;

%let n6=0;

%let n7=*;
66

%put _user_;

/*Delete the macro variables */

syntax: %symdel macro_vars;

%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;

/*%eval & %sysevalf func */

/*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 date=today ();

%put date=%Sysfunc (today ());

%put date=%Sysfunc (today (), date9.);

/*%str & %nrstr func */


70

%let statement=Title " SP 100 ";

%put&=statement;

%let statement=%str(Title " SP 100 " ;);

%put&=statement;

%str : ~, @, #,$,^,*,( ,) _,- ,+,=


;

%let statement=Title " S&P 100 ";

%put&=statement;

%let statement=%str(Title " S&P 100 " ;);

%put&=statement;

%let statement=%nrstr(Title " S&P 100 " ;);

%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

/*Store a macro permanently */

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

/*proc catalog procedure*/


proccatalogcat=ss.sasmacr;
contents;
quit;

********************************;
72

/*macro parameters */

1. positional parameters.
2. keyword parameters.
3. Mixed Parameters.

procprintdata=sashelp.air;
run;

procprintdata=sashelp.class;
run;

procprintdata=sashelp.buy;
run;

%macro prt (dsn);


proc print data=&dsn;
run;
%mend;

%prt (sashelp.air);

%prt (sashelp.class);

%prt (sashelp.buy);

/*positional */

%macro srt (ip,od,var);


proc sort data=&ip out=&od;
by &var;
run;
%mend;

optionsmprint;
%srt (sashelp.class, cls, sex)

/*keyword */
73

%macro srt1 (ip=, od=, var=);


procsort data=&ip out=&od;
by&var;
run;
%mend;

optionsmprintsymbolgen;
%srt1 (od=cls2, ip=sashelp.class, var=sex)

/*Mixed */

%macro srt1 (ip, od=, var=);


procsort data=&ip out=&od;
by&var;
run;
%mend;

optionsmprintsymbolgenmlogic;
%srt1 (sashelp.class, od=cls3, var=sex)

*********************************************;

/*Using Data step to create macro_vari */

Data_null_;
call symput ("x",100);
run;

%put&=X;

Data_null_;
call symput ("x",100);
call symputx ("y",100);
run;

%put&=X &=y;

call symput ("macro_var_name", macro_value);


74

macro_value: numeric, character, expression,


variable_name;

/*1. series of macro macro_variables*/

Datasd;
set sashelp.class;
run;

Datasd;
set sashelp.class;
callsymputx("Nm", name);
run;

Datasd;
set sashelp.class;
call symputx("Nm"||(compress(_N_)), name);
run;

/*2. Find out total observations and store as a macro var */

Data_null_;
set sashelp.class end=cn;
if cn=1thencall symputx("count",_n_);
run;

%put&=count;

/*proc sql to create macro_vari*/

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

You might also like