BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
SeanZ
Obsidian | Level 7

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

3 REPLIES 3
Peter_C
Rhodochrosite | Level 12

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

Tom
Super User Tom
Super User

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

Ksharp
Super User

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

model ret = &x1 -- &x4?

Ksharp

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 3 replies
  • 917 views
  • 0 likes
  • 4 in conversation