BookmarkSubscribeRSS Feed
bollibompa
Quartz | Level 8

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

6 REPLIES 6
LinusH
Tourmaline | Level 20

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
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

Ksharp
Super User

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_;
bollibompa
Quartz | Level 8

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

 

 

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

Astounding
PROC Star

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

 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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
  • 6 replies
  • 1429 views
  • 1 like
  • 5 in conversation