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

Hi,

 

I have a dataset with 5003 variables. 5000 of these are required to call a macro (Var1 to Var5000).

 

How I can I avoid explicitly writing the lines below out every time?

 

%macro_run (Var1);

%macro_run (Var2);

...

%macro_run (Var5000);

 

 

Thanks in advance.

1 ACCEPTED SOLUTION

Accepted Solutions
ed_sas_member
Meteorite | Level 14

proc contents data=have out=want (keep=name where=(name not in ('Date', 'Time', 'Month'))) noprint;
run;

data _null_;
	set want;
	rc = dosubl(cats('%macro_run (',name,');'));
run;

View solution in original post

10 REPLIES 10
ed_sas_member
Meteorite | Level 14

Hi @PetePatel 

You can use the DOSUBL function (or CALL EXECUTE) to execute code immediately:

data list;
	do i=1 to 5000;
		var = cats('Var',i);
		output;
	end;
run;

data _null_;
	set list;
	rc = dosubl(cats('%macro_run (',Var,');'));
run;
Tom
Super User Tom
Super User

You might want to refactor the way the code works to eliminate the need for this, but without details we cannot help with that.

 

You need to get the variable names into a list.  (Note if they are literally VAR1 to VAR5000 then you can just generate them with a DO loop).  So use PROC CONTENTS or the dictionary view/tables.  Then use that data to generate the macro calls.

 

Example:

%let ds=sashelp.class;
proc contents data=&ds out=contents noprint; run;

filename code temp;
data _null_;
  set contents;
  file code;
  put '%macro_run(' name ');' ;
run;
%include code / source2;

Question: How do you know which 3 variables to exclude?

PetePatel
Quartz | Level 8

Thanks, Tom.

 

The variables are named differently but I know I need to exclude date time and month. The rest should be included.

 

So imagine if I have a dataset:

 

Date

Time

Month

GRCV

GHPI_2

SIORP_0

RHP

TRYP

 

How can I call the macro for the last 5 variables only.

 

Currently I am doing:

%Run_Macro (GRCV);

...

%Run_Macro (TRYP);

 

ed_sas_member
Meteorite | Level 14

proc contents data=have out=want (keep=name where=(name not in ('Date', 'Time', 'Month'))) noprint;
run;

data _null_;
	set want;
	rc = dosubl(cats('%macro_run (',name,');'));
run;
PetePatel
Quartz | Level 8

Thanks!

ed_sas_member
Meteorite | Level 14

Here is one approach to retrieve the last five variables of your dataset:

proc contents data=have out=want (keep=name varnum where=(name not in ('Date', 'Time', 'Month'))) noprint;
run;

data list;
	if _n_=1 then do;
		declare hash h (dataset:'want', ordered:'d');
		h.definekey('VARNUM');
		h.definedata('VARNUM','NAME');
		h.definedone();
		declare hiter C('h');
	end;
	set want;
	C.first();
	output;
	do i=1 to 4;
		C.next();
		output;
	end;
	stop;
run;
Tom
Super User Tom
Super User
where upcase(name) not in ('DATE' 'TIME' 'MONTH');
PaigeMiller
Diamond | Level 26

@PetePatel wrote:

Thanks, Tom.

 

The variables are named differently but I know I need to exclude date time and month. The rest should be included.

 

So imagine if I have a dataset:

 

Date

Time

Month

GRCV

GHPI_2

SIORP_0

RHP

TRYP

 

How can I call the macro for the last 5 variables only.

 

Currently I am doing:

%Run_Macro (GRCV);

...

%Run_Macro (TRYP);

 


Again, depending on what %Run_Macro does, you may not need macros at all

 

proc contents data=have out=_contents_ noprint;
run;
proc sql noprint;
    select distinct name into :names separated by ' ' from _contents_
        where upcase(name) not in ('DATE','TIME','MONTH');
quit;
proc summary data=have;
    var &names;
    output out=stats mean= std=/autoname;
run;
--
Paige Miller
PaigeMiller
Diamond | Level 26

Depending on what %macro_run does, you may not need macros at all.

 

Example:

 

proc summary data=abc;
    var var1-var5000;
    output out=_stats_ mean= std= /autoname;
run;
    
--
Paige Miller
gamotte
Rhodochrosite | Level 12

Hello,

 

You can loop on the dataset columns names with "call vnext" and execute the macro with "call execute" as follows :

 

Edit: added "stop;".

 

%macro mymacro(vname);

    %put Macro execution for variable &vname.;

%mend;

data have;
   input a b c d e f g h i j;
   cards;
1 2 3 4 5 6 7 8 9 0
;
run;

data _NULL_;
    set have;
    length _VNAME_ $32.;

    do while(_VNAME_ ne "_VNAME_");
        call vnext(_VNAME_);
        if _VNAME_ not in ("h" "i" "j" "_VNAME_") then 
            call execute(cats('%nrstr(%mymacro)(',_VNAME_,')'));
    end;

    stop;
run;

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
  • 10 replies
  • 997 views
  • 8 likes
  • 5 in conversation