SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Emma_at_SAS
Lapis Lazuli | Level 10

I wrote a macro and not I want to run it for several variables. Some variables have the same prefix for example

var_name_d1 

var_name_d2 

var_name_d3 

var_name_d4 

var_name_d99 

var_name_d999

I am not sure if they are in the same order in the dataset or if some other column may be in between these columns.

1- Can I write a short form to feed these 6 variables to my macro at one step?

2- If yes, how may I do that?

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Sounds like you need to either modify your macro or create a new one to call the old one.

So assume your macro calls to the old macro look like %old(dsn=sashelp.class,var=height) then your new code could look like this.  (Note the macro wrapper is not really needed).

%macro new(prefix,dsname);
proc contents data=&dsn(keep=&prefix:) noprint out=names(keep=name); run;
data _null_;
  set names ;
  call execute(cats('%nrstr(%old)(dsn=',"&dsn",',var=',nliteral(name),')'));
run;
%mend;

 

View solution in original post

7 REPLIES 7
PaigeMiller
Diamond | Level 26

Without seeing the macro you wrote, it's nearly impossible to give you advice. What does the macro do?

--
Paige Miller
Emma_at_SAS
Lapis Lazuli | Level 10
Thank you, PaigeMiller. Based on the next suggestions, I noticed what you mean.
Thanks
PaigeMiller
Diamond | Level 26

@Emma_at_SAS wrote:
Thank you, PaigeMiller. Based on the next suggestions, I noticed what you mean.
Thanks

Depending on what you are doing, the solution marked correct (from @Tom ) may be overkill. You may not need a loop or CALL EXECUTE at all, depending on what analysis you are doing with these variables. 

--
Paige Miller
Kurt_Bremser
Super User

No. The macro processor only deals with code, but the structures of datasets (and therefore the variables) become only evident when the code (created by the macro) later runs.

But you can build a macro variable from dictionary.columns that contains all the variable names and can be used as a macro parameter.

Reeza
Super User
Look into CALL EXECUTE for ways to automate your macro call.
Tom
Super User Tom
Super User

Sounds like you need to either modify your macro or create a new one to call the old one.

So assume your macro calls to the old macro look like %old(dsn=sashelp.class,var=height) then your new code could look like this.  (Note the macro wrapper is not really needed).

%macro new(prefix,dsname);
proc contents data=&dsn(keep=&prefix:) noprint out=names(keep=name); run;
data _null_;
  set names ;
  call execute(cats('%nrstr(%old)(dsn=',"&dsn",',var=',nliteral(name),')'));
run;
%mend;

 

Emma_at_SAS
Lapis Lazuli | Level 10

Thank you everyone! 

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 1413 views
  • 3 likes
  • 5 in conversation