04-11-2017 03:10 AM
I have a trouble understanding a code like below:
%let hi=&hi test_&a;
what does the code in underline do?
04-11-2017 03:37 AM
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).
04-11-2017 05:40 AM
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)