BookmarkSubscribeRSS Feed
irisirissy
Calcite | Level 5

I have a trouble understanding a code like below:

 

%let hi=;

%macro hi(a);

data test_&a;

set kk;

where class=a;

run;

 

%let hi=&hi test_&a;

%mend;

 

%hi(1)

%hi(2)

%hi(3)

 

what does the code in underline do? 

 

2 REPLIES 2
PeterClemmensen
Tourmaline | Level 20

It is simple macro variable definition. it Overrides the macro variable set at the top by the same name of hi and adds another string containing the string 'test_' and the macro variable a, which is the parameter given to the macro in the parenthesis in hi(a).

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Its really not very good programming.  Several reasons.  First the macro really doesn't do anything other than split your data out - which is rarely a good idea anyway, but in doin git this way obfuscates the code somewhat:

data test_1 test_2 test_3;
  set kk;
  select(a);
    when 1 output test_1;
    when 2 output test_2;
    otherwise output test_3;
  end;
run;

Is effectively the same.

The code is also incorrect as you give it, unless there is a variable called a in the dataset kk, it will not work, I assumed you meant &a?

 

Now if you add a %put statement into the macro you will clearly see what the %let is doing, it is building a list of space delimited dataset names e.g.

test_1 test_2 test_3

I assume this is so you can use that list to process further - again, because of the programming choice to split your data up you then have to start adding lots of code to try to process multiple datasets - more work, harder to maintain etc.  Use one dataset and use by group processing - this is what it is for,

Finally, you are telling SAS that the hi macro is global, and then adjusting it within a macro.  In this simple example it may work, but doing something like this can lead to very hard to debug problems later on - when code may not even be in the same file etc.  Again, not a good idea.  Unless there is a good reason, follow encapsulisation and keep scope local.

This is the program (with some readability added - very important):

%let hi=;
%macro hi(a);
  data test_&a.;
    set sashelp.class;
    where age=&a.;
  run;
   
  %let hi=&hi test_&a.;
  %put &hi.;
%mend;
 
%put &hi.;

%hi(12)
%hi(15)
%hi(16)
 

 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 384 views
  • 1 like
  • 3 in conversation