DATA Step, Macro, Functions and more

creating macro-var from all variables in a dataset

Reply
Contributor
Posts: 73

creating macro-var from all variables in a dataset

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

Super User
Posts: 5,258

Re: creating macro-var from all variables in a dataset

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?

 

Data never sleeps
Super User
Super User
Posts: 7,411

Re: creating macro-var from all variables in a dataset

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.

Super User
Posts: 9,682

Re: creating macro-var from all variables in a dataset

[ Edited ]

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_;
Contributor
Posts: 73

Re: creating macro-var from all variables in a dataset

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

 

 

Super User
Super User
Posts: 7,411

Re: creating macro-var from all variables in a dataset

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.

Super User
Posts: 5,085

Re: creating macro-var from all variables in a dataset

You can iterate using a single macro variable.  Here's a reference with some details:

 

http://blogs.sas.com/content/publishing/2015/01/30/sas-authors-tip-getting-the-macro-language-to-per...

 

Ask a Question
Discussion stats
  • 6 replies
  • 283 views
  • 1 like
  • 5 in conversation