Hi,
I have a dataset with about 50 variables with one value in each variable.
Is there any easy way to create 50 macro-variables of these variables, like:
Proc sql:
select *
into: VAR1-VAR50
Many thanks in advance!
Thomas
For starters, having 50+ variables is not very convenient, which you now experiencing...
Use an array in the data step, and do call symput.
But again, it's quite inconvenient to handle 50+ macro variables.
Whats the use case?
Why would you want to do this? Macro language is not for processing data, that is what Base SAS is for. Trying to do things this way will lead to messsy hard to read, and hard to maintain code which does nothing a simple datastep would not do. Post some example test data (as a datastep), and what you would like to achieve and we can show you some code.
Here is an example :
proc transpose data=sashelp.class(obs=1) out=temp;
var _all_;
run;
data _null_;
set temp;
call symputx(_name_,col1);
run;
%put _user_;
Thanks!
I agree that many macro-var is not convinient. However my situation is the following:
I have a macro which I want to run for about 50 variables. The macro calculates standardized differences for one variable each run.
macro std_diff;
xxxx
yyyy
%mend std_diff;
%std_diff(AGE);RUN;
%std_diff(CHF);RUN;
%std_diff(BB);RUN;
and so on...
Of course I can write all 50 variables here BUT my issue is that every time I update my model with new data, some variables might not have enough data to run the macro(calculate the standardized difference). For example, it might be that the variable CHF can not be calculetd in som runs. In these situation the macro will terminate.
So I am thinking of programming some dynamic thing here which tell the macro which variables to be run.(I now in each update which variables possible to run).
Thanks for any advice!
Thomas
You have a whole load of options here. Firstly, consider putting another variable in your data, and using that as a by group, it will be the easiest to code and maintain and run quickest. E.g.:
data have is:
BYVAR your variables
ABC
ABC
ABC
DEF
...
Then:
proc means data=have; by byvar; ... run;
This would always be my number one option, and should cover most events.
You could also do this:
data _null_; do i="var1","var2","var3"; call execute('proc means data=have; var='||strip(i)||'; run;'); end; run;
You could also transpose your data, and use arrays. With some proper test data in the form of a datastep i can provide code.
You can iterate using a single macro variable. Here's a reference with some details:
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.
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.