DATA Step, Macro, Functions and more

Define macro variables

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 122
Accepted Solution

Define macro variables

I defined 15 macro variables like

%let x1=xxx

%let x2=xxx

%let x3=xxx

....

xxx are some variables in my dataset.

I wonder how I can call some of them together in some procedures such as proc sql and proc reg. For example, I can write

proc reg data=new;

     model ret = &x1 &x2 &x3 &x4;

run;

Can I shorten this code such as model ret = &x1-&x4? I tried this, but not work.

Thanks.


Accepted Solutions
Solution
‎07-17-2013 06:43 PM
Super User
Super User
Posts: 7,079

Re: Define macro variables

If you are already running in a macro then use a DO loop.

%do i=1 %to 15 ; &&x&i %end;

Or just make a new macro variable. You still have to type them out but only one time.

%let xall=&x1 &x2 &x3 &x4 &x5 &x6 .......... ;

Or pull the names from the metadata? Watch out for the SCOPE setting.

proc sql ;

   select distinct cats('&',name) into : xall separated by ' '

   from dictionary.macros

   where name like 'X%' and scope = "GLOBAL"

   ;

quit;

.... var &xall ... ;

View solution in original post


All Replies
Valued Guide
Posts: 2,177

Re: Define macro variables

some functions and statements allow variable ranges. The rules are not in the macro language

Solution
‎07-17-2013 06:43 PM
Super User
Super User
Posts: 7,079

Re: Define macro variables

If you are already running in a macro then use a DO loop.

%do i=1 %to 15 ; &&x&i %end;

Or just make a new macro variable. You still have to type them out but only one time.

%let xall=&x1 &x2 &x3 &x4 &x5 &x6 .......... ;

Or pull the names from the metadata? Watch out for the SCOPE setting.

proc sql ;

   select distinct cats('&',name) into : xall separated by ' '

   from dictionary.macros

   where name like 'X%' and scope = "GLOBAL"

   ;

quit;

.... var &xall ... ;

Super User
Posts: 10,046

Re: Define macro variables

If their order is the same within dataset . or you could try this.

model ret = &x1 -- &x4?

Ksharp

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 186 views
  • 0 likes
  • 4 in conversation