- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Without seeing the macro you wrote, it's nearly impossible to give you advice. What does the macro do?
Paige Miller
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Thank you everyone!