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



%let hi=&hi test_&a;







what does the code in underline do? 


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).

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;
    when 1 output test_1;
    when 2 output test_2;
    otherwise output test_3;

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.;
  %let hi=&hi test_&a.;
  %put &hi.;
%put &hi.;



Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 1 like
  • 3 in conversation