BookmarkSubscribeRSS Feed
lu2kaseff
Calcite | Level 5

heres is the code:

data makehospnum;

input rownum hospnum;

datalines;

1 5

2 8

3 9

4 10

5 11

6 12

7 13

8 19

9 21

10 25

11 30

12 41

13 42

14 43

15 44

16 98

17 99

run;

data _null_;

set makehospnum;

call symput("nobs", compress(_n_));

run;

 

 

%macro doit;

%global runnum;

%do K = 1 %to &nobs;

data _null_;

set makehospnum;

if rownum = &k then call symput("runnum", compress(hospnum));

run;

%do_zscore(infile = ed_data_temp, hospvar= &runnum , numlags= 30);

%put hospnum = &runnum;

%end;

%mend doit;

%doit;

 

 

I was wondering instead of looping over the hospital numbers could i use a call execute command.

 


thank you,

 

LK

5 REPLIES 5
Reeza
Super User

Yes you can use Call Execute. The documentation has a really good example of data driven execution, see Example 2.

 

http://support.sas.com/documentation/cdl/en/mcrolref/69726/HTML/default/viewer.htm#n1q1527d51eivsn1o...

ballardw
Super User

Something like this?

data _null_;
   set makehospnum;

   call execute('%do_zscore(infile = ed_data_temp, hospvar='||hospnum||', numlags= 30);');

run;

Here you want the macro call inside single quotes to keep it from attempting to resolve within the data step.

 

Astounding
PROC Star

Unfortunately, SAS makes you jump through additional hoops to suppress macro activity within CALL EXECUTE.  You may need to switch to:

 

call execute('%nrstr(%do_zscore(infile = ed_data_temp, hospvar='||hospnum||', numlags= 30);)');

 

Alternatively, you could simplify your original loop slightly by switching to:

 

data _null_;

   set makehospnum (firstobs=&k obs=&k);

   call symputx("runnum", hospnum);

run;

 

That way, the %PUT statement runs in its proper place, something that is more difficult to accomplish with CALL EXECUTE.

 

Tom
Super User Tom
Super User

Yes.

data _null_;
  set makehospnum;
  call execute(catx(' ','%nrstr(%do_zscore)(infile = ed_data_temp,'
    ,'hospvar=',hospnum,',numlags=30);'
  ));
run;

Or just write the code to a file and include it.  if you dataset variable names match your macro parameter names you can use the VAR= syntax in the PUT statement to write both the parameter name and the value.  To demonstrate let me rename your dataset variable.

filename code temp;
data _null_;
  set makehospnum(keep=hospnum rename=(hospnum=hospvar));
  put '%do_zscore(infile=ed_data_temp,numlags=30,' hospvar= ');' ;
run;
%include code /source2;
rogerjdeangelis
Barite | Level 11
Does not exactly answer your question but might help

Means for variables defined in a meta dataset

inspired by
https://goo.gl/YZ0QDK
https://communities.sas.com/t5/Base-SAS-Programming/call-execute/m-p/338992


HAVE the following meta datasets
=================================

 up to 40 obs from meta total obs=2

 bs     VAR

 1     HEIGHT
 2     WEIGHT

and sashelp.class


p to 40 obs from sashelp.class total obs=19

bs    NAME       SEX    AGE    HEIGHT    WEIGHT

 1    Alfred      M      14     69.0      112.5
 2    Alice       F      13     56.5       84.0
 3    Barbara     F      13     65.3       98.0
 4    Carol       F      14     62.8      102.5
 5    Henry       M      14     63.5      102.5
 6    James       M      12     57.3       83.0
 7    Jane        F      12     59.8       84.5
 8    Janet       F      15     62.5      112.5

WANT (proc means for variables in meta data)
==============================================

                    Analysis Variable : HEIGHT

 N            Mean         Std Dev         Minimum         Maximum
------------------------------------------------------------------
19      62.3368421       5.1270752      51.3000000      72.0000000
------------------------------------------------------------------



                   Analysis Variable : WEIGHT

N            Mean         Std Dev         Minimum         Maximum
-----------------------------------------------------------------
9     100.0263158      22.7739335      50.5000000     150.0000000
-----------------------------------------------------------------

WORKING CODE
============

     call symputx('hvar',hospvar);
     rc=dosubl('
       %do_zscore(infile=sashelp.class,numlags=30,hospvar=&hvar);
     ');

FULL SOLUTION
=============

%symdel hvar; * just in case it exists;
proc datasets lib=work;
delete meta;
run;quit;

data meta;
   do var='HEIGHT','WEIGHT';
     output;
   end;
run;quit;

* SAS solution;

%macro do_zscore(infile = ed_data_temp,hospvar=,numlags=30);
   proc means data=&infile.;
   var &hospvar.;
   run;quit;
%mend do_zscore;

data _null_;
   set meta(keep=var rename=var=hospvar);
   call symputx('hvar',hospvar);
     rc=dosubl('
       %do_zscore(infile=sashelp.class,numlags=30,hospvar=&hvar);
     ');
run;quit;

or


data _null_;
   set meta(keep=var rename=var=hospvar);
   call symputx('hvar',hospvar);
     rc=dosubl('
        proc means data=sashelp.class;
        var &hvar.;
        run;quit;
     ');
run;quit;

You could easily add infile macro var to meta data

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 5 replies
  • 1586 views
  • 0 likes
  • 6 in conversation