Macro Functions (or) String Functions in SAS

Macro Functions (or) String Functions

It requires operands (variable) Eg: int(var), length(var). In Macros, we can write functions inside or outside of the macroblock (global function).

a) % length: Using this function, we can report length of the macrovariable.

Eg: /* Report */ 

% global dnames;

% let dnames=demo lab medi adevent;

% put Mvar length is % lengt (&dnames);

/* storage */

% global len;

% let len=%length (&dnames);

b) % index: Using this function, we can report specific character position in macrovariable.

Eg: /* Report */

% global dnames;

% let dnames=demo lab medi adevent;

% put char position is

% index (&dnames, m);

c) % scan: Using this function, we can get required word from string.

Eg: /* Report */

% global dnames;

% let dnames=demo lab medi adevent;

% put Rq word is

% scan (&dnames, 3);

d) % Upcase: It shows required in capital letters.

Eg: /* Report */

% global dnames;

% let dnames=demo lab medi adevent;

% put Rq string is

% Upcase (&dnames);

e) % Lowercase: It shows required data in small letters.

Eg:         

% put Rq string is

% Lowercase (&dnames);

f) % substring: We can get part of the string from macrovariable. Here also internally % eval is working.

% put Rq string is

% substr (&dnames, 6, 10);

Note: % scan and % substr these two functions internally work with % eval function or expression.

Enthusiastic about exploring the skill set of SAS? Then, have a look at the blog post of  

SAS Training in Delhi to gather additional knowledge.

Concatination Of Macrovariables

Eg:         

% global surname name;

% let surname=kolla;

% let name=lava kumar;

% global fname;

% let fname=&surname  &name;

% put  &fname;

Logical Expressions

  1. % if % then   % else
  2. % if % then  % else  % if ...... % else

Eg: 

% macro sprint (rq, ename, new, var);

% if  % Upcase(4rq)= SORT  % then  % do;

proc sort data=&ename  out=&new;

by  & var;

run;

% end;

% else    % do;

proc print data = & ename;

run;

% end;

% mend;

/* Calling */

% sprint (   , sasuser.medi1);  /* report */

% sprint (sort, sasuser.medi1, medi3, sbp); /*sorting */

% sprint (   , medi3);

% sprint ( sort, sasuser.medi1, medi4, descending sbp);

Eg:

% macro dmanage (rq, dname, enames, var);

% if  % Upcase (&rq)=MERGE  % then  % do;

data  &dname;

merge &enames;

by  & var;

run;

% end;

% else    % if   % Upcase(&rq)=UPDATE

% then   % do;

data  &dname;

Update  &enames;

by  & var;

run;

% end;

% else   % if   % Upcase(&rq)= MODIFY  % then   % do;

data  &dname;

Modify  &enames;

by  & var;

run;

% end;

% else   % do;

proc sort data = &enames

out = &dname;

by  & var;

run;

% end;

% mend;

% macro print (dname);

proc print data = &dnmae;

run;

% mend;

data medi;

input pid drug 7 sbp;

cards;

100   col5mg   156

101   col10mg   167;

data success;

input pid sbp;

cards;

101   145

100   150;

/* Calling */

% damange (   , medi1, medi, pid);

% damange (   , success1, success, pid);

% damange ( Update, medi2, medi1 success1, pid);

% print (medi2);

Eg:

data emp1;

input eid salary;

cards;

100   2300

110   4500

230   5600

;

data emp2;

input eid istage;

informat istage percent4;

cards;

230   30%

110   20%

100   10%

;

options mprint;

% damange (   , emp3, emp1, eid);

% damange (   , emp4, emp2, eid);

% damange (Modify, emp3, emp3 emp4,

% str (eid, salary=salary+(salary * istage));

% print (emp3);

Eg:

data medi;

input pid drug & sbp;

cards;

100   col5mg   156

101   col10mg   167

;

data adevent;

input pid adtype &;

cards;

101   Eyedis

100   Eardis;

% damange (   , medi1, medi, pid);

% damange (   , adevent1, adevent, pid);

% damange ( Merge, mead, medi1 adevent1, pid);

% print (mead);

% Do % while loop:

mlogic option:

It can be used to trace out logical expressions. Default nomlogic

% global dname;

% let dnmae=emp1  emp2  emp3  emp4  medi;

% macro down;

% local dat 1;

% let i=1;

% do   % while (&i <=5);

% let dat=% scan (&dname, &i);

proc print data =&dat;

run;

% let i =% eval (&i + 1);

% end;

% mend;

options mprint symbologen mlogic;

% do wh;

Eg:

% macro down1;

% local dat 1;

% let i=1;

% let  data=% scan (&dname, &i); - loop entering statement

% do   % while (&dat ne);

proc print data =&dat;

run;

% let i =% eval (&i + 1);

% let dat=%scan(&dname, &i); - loop running

% end;

% mend;

options mprint symbologen mlogic;

% do wh1;

Go To Block (or) Statement

This statement is working based on label statement and run group of required statements.

Label Statement: This statement indicate group of statements.

- If we want to run 'go to' statement we will use conditional block 'if'.

Eg:

% macro gto (rq, dname, enames, var);

% if   % Upcase (&rq) =SORT  % then

% goto sort;

% else   % if   % Upcase (&rq)=MERGE

% then   % goto merge;

% else   % goto pront;

% sort  :  proc sort data=&enames

out = &dname;

by & var;

run;

% goto ext;

% print : proc print data=&dname;

run;

% goto ext;

% merge : data &dname;

merger &enames;

by & var;

run;

% goto ext;

% ext : % mend;

/* calling */

% gto (   , sasuser.demo);  /* reoprt */

% gto (sort, demo1, sasuser.demo, gender); 

/* sorting */

% gto (   , demo1);

Eg:         

data demo;

input pid age;

cards;

100   78

101   90

;

data medi;

input pid drug & ;

cards;

101   5mg

100   10mg

;

% macro dmanage (new, enames, var);

data &new;

& enames;

by  & var;

run;

% mend;

% gto (sort, demo1, demo, pid);

% gto (sort, medi1, medi, pid);

% dmanage (dmedi, merge demo1 medi1, pid);

% gto (   , dmedi);

For indepth knowledge on SAS, click on below