Hello, I want to resolve a series of macro variables in a do loop, but the following program doesn't work. For some reason, I can't
define the array for mac1-mac3. Any idea on how to resolve them directly?
%let mac1=2;
%let mac2=3;
%let mac3=7;
data test;
array gg{3} gg1-gg3 ;
do i=1 to 3;
gg{i}=dequote(cats('&','mac',put(i,best.)));
end;
run;
%let mac1=2;
%let mac2=3;
%let mac3=7;
data test;
array gg{3} gg1-gg3 ;
do i=1 to 3;
gg{i}=symgetn(cats('mac',i));
end;
run;
%let mac1=2;
%let mac2=3;
%let mac3=7;
data test;
array gg{3} gg1-gg3 ;
do i=1 to 3;
gg{i}=symgetn(cats('mac',i));
end;
run;
Why would you put numerical data into character macro variables, then write a datastep to put them into a series of numerical variables in a dataset using implicit conversion? The whole process seems bizarre?
data test; gg1=1; gg2=2; gg3=3; run;
This is effectively what the code does?
@liyongkai800 wrote:
The reason is that I calculate those macro variables in the earlier step and want to use them in later data step. My real program is like
data want;
set one;
Impose the do loop on set one(include that series of macro variables)
That might be bizarre for you, but as a fresh sas man, I can't come up with a better idea.
Anytime you have a series of data, consider to store it in a dataset. If you need it for look-up purposes, create a format from that dataset.
I basically only store singular pieces of data (like a cutoff date, or a company descriptor for filtering) in macro variables.
Think like that: the macro preprocessor helps you in making code dynamic. It is not designed (or meant) for handling data.
Just to add to that, you also use the ability to merge/join data as well.
So if you have single observation dataset with those three variables you can include into a future and there is no need to move the values into macro variables at all.
Say you have the values in a dataset name SUMMARY and you want to combine that with another dataset named HAVE to produce a dataset named WANT. You might code something like this:
data want ;
if _n_=1 then set summary ;
set have ;
* Calculations using variables from HAVE and SUMMARY ;
run;
SAS will automatically retain the values of the variables from SUMMARY onto all iterations of the data step.
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.