Interface functions are 2 types.
Syntax: call symput ("Macrovariable"(name), dataset var name);
Eg: data one;
group =100;
drug catalog;
run;
data_null_;
set one;
call symput ('var1', group);
call symput ('var2',drug);
run;
opitons symbolgen;
% put &var1 &var2;
Using this function, we can create dataset variables from the macrovariables during datastep execution.
Syntax: Symget9"Macrovariable");
Eg:
data medi;
Gid-symget ("var1");
Ndrug=symget("var2");
run;
proc print data=medi;
run;
Eg:
data medi2;
input pid drug &;
cards;
100 col5mg
101 col10mg
102 col15mg
;
data_null_;
set medi2;
call symput ('var3', drug);
run;
% put 7var3;
These core tutorials helps you to learn the Macro Interface Functions In SAS. For in-depth knowledge and practical experience explore SAS Online Training.
Note: If dataset has multiple values call symput function default stores last data value in macrovariable
Using this function, we can report required macrovariable existed or not. This function we can run by using condition.
Syntax: symexist ('Macrovariable');
Eg:
data_null_;
if symexist ('var6'); then
put 'Macrovariable is existed';
else
put 'Macrovariable does not exist';
run;
- rawdata (sheet1)
center pid age drug date group
Appolo 100 27 5mg 12/23/2005 G100
Appolo 109 23 5mg 12/23/2005 G100
Appolo 108 34 10mg 12/24/2005 G100
Appolo 109 22 5mg 12/23/2005 G100
Appolo 118 45 15mg 12/25/2005 G100
Appolo 134 49 15mg 12/25/2005 G100
(sheet2)
center group Age pid
Appolo G200 26 190
Appolo G200 22 136
Appolo G200 37 156
Appolo G200 49 189
Appolo G200 43 123
Appolo G200 42 150
1st Import the data
data_null_;
set medi1;
if age >=20 and age <=30 then do;
call symput ('var1', drug);
call symput ('D1', date);
end;
else if age > 30 and age <=40
then do;
call symput ('var2', drug);
call symput ('D2', date);
end;
else do;
call symput ('var3', drug);
call symput ('D3', date);
end;
run;
% put &var1 &var2 &var3;
% put &d1 &d2 %d3;
data medi3;
set medi2;
if age >=20 and age <=30 then do;
drug = symget ('var1');
date = symget ('D1');
end;
else if age > 30 and age <=40
then do;
drug = symget ('var2');
date = symget ('D2');
end;
else do;
drug = symget ('var3');
date = symget ('D3');
end;
date1 = input (date, best12);
drop date;
rename date1 = date;
run;
proc print data = medi3;
format date date9;
footnote."Drug information &var1-&var2-&var3";
run;
call execute: Using call execute function, we can call required catalog (macrocall) from th dataset block.
Syntax: call execute ('%<macroname>');
%sysfunC: Using this function, we can call dataset functions in macros.
% global varB;
% let varB=89.67;
% global varB1;
% let varB1=%sysfnc(int(&varB));
% put &varB1;
a) exist: Using this function, we can report required SAS file is existed or not. If it is existed it returns '1' otherwise 'Zero'.
Syntax: Exist ('Datasetname');
b) file exist: Using this function, we can report pc files are existed or not. If it is existed it returns '1' otherwise 'zero'.
Eg:
data_null_;
of exist ('medi1')=1 then
put 'dataset is existed';
else put 'dataset does not exist';
run;
c) Open Function: Using this function, we can open the required dataset internally.
Syntax: open ('datsetname');
d) Attrn: Using this function, we can count no of variables and no of observations using open results.
Syntax: Attrn (open result, 'nvars' or 'nobs');
e) close: Using this function, we can close the dataset.
Syntax: close (open result);
Eg:
data_null_;
if exist ('sasuser.demo')=1
then do;
OP=open('sasuser.demo');
NV=attrn(OP, 'NVARS');
NO=attrn(OP, 'NOBS');
CL=close(OP);
put 'dataset is existed';
put ' ';
put ' ';
put 'No of variables _ _'NV;
put 'No of Obs _ _ _'NO;
end;
else put 'dataset does not exist';
run;
(same coding in macros)
% macro dex(dname);
% if % sysfunC(exist(&dname))=1
% then % do;
% local op nv nob cl;
% let op=%sysfunc(open(&dname));
% let nv=%sysfunc(attrn(&op, nvars));
% let nob=%sysfunc(attrn(&op, nobs));
% let cl=%sysfunc(close(&op));
% put &dname dataset is existed with 7nv variables &nob observations;
% end;
% else;
% put &dname dataset is not existed;
% mend;
% dex(sasuser.lab);
% dex(sasuser.demo);
Query (or) select statement and into clause: Using these 2 options, we can create macrovariable from the dataset variable.
Here SAS system default stores 1st data value (or) first occurence in macrovariable.
Eg:
data medi;
input group & visit drug &;
cards;
G100 1 col5mg
G200 1 col10mg
G300 1 col15mg
G100 2 col5mg
G200 2 col10mg
G300 2 col15mg
;
proc sql noprint;
select drug into : med 1
from medi
quit;
% put & med1;
Eg:
proc sql noprint;
select distinct(drug) into : medicine1-:medicine3
from medi;
quit;
% put &medicine1 &medicine2 &medicine3;
titile "Drug information &medicine1 - &medicine2 - &medicine3";
proc sql;
select * from medi;
quit;
Eg:
proc sql noprint;
select distinct(group) into : G1-: G3 from medi;
quit;
title1 "Group information is &G1 - &G2 - &G3";
title2 "Drug information &medicine1 - &medicine2 - &medicine3";
proc sql;
select * from medi;
quit;
_Global_: Using this statement, we can report list of the macrovariables (global macrovariables) with values. This statement can be written anywhere in SAS application.
Eg:
% global var1 var2 var3;
% let var1 = 100;
% let var2 = col5mg;
% let var1 = 23;
% put _global_;
_Local_: Using this statement, we can report list of the local macrovariables. This statement can be written inside of the macroblock.
Eg:
% local pid age center;
% let pid = 200;
% let age = 45;
% let center = appolo;
% put _local_;
% mend;
%mvar;
_User_: Using this statement, we can report user defined macrovariable. If we write inside of the macrobloxk, it reports both global; and local macrovariables. If we write outside of the macroblock, it report only global macrovariables.
Eg:
% put _User_;
_Automatic_:
Using this statement, we can report list of automatic macrovariable.
Eg:
% put _automatic_;
Sysdate: It report current operating system date.
Eg: options nodate;
title1 'Drug information';
title2 "Report date-&sysdate9-&systime";
proc print data = saashelp.class;
run;
Systime: It report current operating system time (24hrs format)
Syslast: It report recently existed dataset.
Eg: data demo;
x = 78;
run;
% put &syslast;
- Default value of the syslast is _null_
Sysdsn: It report recently existed dataset & current working library.
Eg: % put &sysdsn;
Sysncpu: It report number of cpu's connected to pc.
Eg: % put &sysncpu;
Sysuserid: It report user id.
Eg: % put &sysnuserid;
Sysver: It report current SAS version.
Eg: % put &sysver;
Sysvlong: It report full of information for current SAS version.
Eg: % put &sysnvlong;
Note: If we use same name for global and local macrovariables, inside it works like a local macrovariable and outside it works like a global macrovariable.
Eg: % global pid;
% let pid = 100;
% macro loc;
% local pid;
% let pid = 200;
% put &pid;
% mend;
% loc;
% put & pid;
Eg: % global pid;
% let pid = 100;
% macro loc;
% let pid = 200;
% put &pid;
% mend;
% loc;
% put & pid;
Sysmax: Using this statement, we can delete macrovariable from the SAS environment.
Eg: % global pid age center;
% let pid = 200;
% let age = 34;
% let center = appolo;
% put _global_;
% symdel age;
% put _global_;
Sysexec: using this function, we can manage operating system environment.
For storage: mstored SASMstore=<Autocall library>;
Using these 2 options, we can store macro catalog in required library. Here SAS Mstore option indicate autocall library.
Eg: Opitons mstored
sasmstore = sasuser;
% macro print (fname)/store;
proc print data = &dname;
run;
% mend;
Calling:
SAS Autos: Using this options, we can indicate autocall library for calling.
/* Calling */
Options sasautos = sasuser;
% print (sasuser.demo);
Arrays: Using arrays concept, we can run same action for required variables.
- Array statement starts with array keyword & requires name of the array, length of the array, variables list.
Eg: data demo;
input pid age gender & race & height weight;
cards;
100 23 female Asian 4.5 56
101 23 male Asian 5.6 56
102 56 female Asian 5.6 56
103 23 female African 5.6 56
;
data demo1; <-- one dimensional explicit array
set demo;
array apple(3) age height weight;
do i = 1 to 3;
if apple(i) = then apple(i) = 0;
end;
array orange(2) & gender race;
do i = 1 to 2;
if orange(i) = ' ' then
orange(i) = 'Miss';
end;
drop i;
run;
proc print;
run;
- Arrays are 2 types
1) One Dimensional Array: is of 2 types
a)Explicit Array
b)Implicit Array
a) Explicit Array: It is working based on length of the array & loop variables.
Syntax: Array <Array name. <length of array> <data type> <variable list>;
b) Implicit Array: It is working based on array names with do over statement.
Syntax: Array <Array name> <data type> <variable list>;
Eg: data demo2;
set demo;
array apple age height weight;
do over apple;
if apple = then apple = 0;
end;
array orange & gender race;
do over orange;
if orange = ' ' then
orange = 'Miss';
end;
run;
proc print data = demo2;
run;
- Explicitly array is more efficient than implicity array.
- To run array, we should use special numeric and character varaibles.
It requires only one argument, the argument must be array name & it returns length of the array.
Eg: data demo3;
set demo;
array apple(*) _numeric_;
do i = 1 to dim(apple);
if apple(i) = then apple(i) = 0;
end;
array orange(*) _char_;
do i = 1 to dim(orange);
if orange(i) = ' ' then
orange(i) = 'Miss';
end;
import the data into the table (sheet2)
proc import
data file = 'd:\ condata.xls'
out = two dbms = excel replace;
sheet = 'sheet2&';
run;
proc sql;
describe table two;
quit;
To create a data set with not null constraint
proc sql;
create table demo1 (pid num not null, age num, gender char, race char);
quit;
proc sql;
describe table demo1;
quit;
proc append base = demo1
data = two force;
run;
Check: Sheet3
Eg: pid age gender race
100 56 female Asian
101 45 female African
102 46 female African
103 34 male Asian
Step1: Importing the data into SAS dataset name3
Step2: To create a table demo2 with check constraint.
Eg: proc sql;
create table demo2 (pid num, age num, gender char, check(gender = 'female'), race char);
quit;
proc sql;
describe table demo2;
quit;
/* Loading */
proc append base = demo2
data = three force;
run;
Note: If we use multiple constraints in table, in loading time each and every observation must be satisfied all constraints in table.
Raw data (sheet4):
pid age gender race
100 56 female Asian
101 45 male African
101 46 female African
102 34 male Asian
103 female Asian
104 56 Asian
104 male African
105 45 female Asian
Step1: Importing the data into SAS dataset name4
Step2: To create a table with multiple constraint for multiple variable.
Eg: Step1 /* Extraction */
step2 /* transformation */
proc sql;
create table demo4(pid num unique, age num not null, gender char not null, race char);
quit;
proc sql;
describe table demo4;
quit;
drop i;
run;
proc print data = demo3;
run;
You liked the article?
Like : 0
Vote for difficulty
Current difficulty (Avg): Medium
1/15
TekSlate is the best online training provider in delivering world-class IT skills to individuals and corporates from all parts of the globe. We are proven experts in accumulating every need of an IT skills upgrade aspirant and have delivered excellent services. We aim to bring you all the essentials to learn and master new technologies in the market with our articles, blogs, and videos. Build your career success with us, enhancing most in-demand skills in the market.
Stay Updated
Get stories of change makers and innovators from the startup ecosystem in your inbox