DATA Step, Macro, Functions and more

call execute

Reply
New Contributor
Posts: 4

call execute

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

Super User
Posts: 17,868

Re: call execute

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

Super User
Posts: 10,516

Re: call execute

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.

 

Super User
Posts: 5,085

Re: call execute

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.

 

Super User
Super User
Posts: 6,502

Re: call execute

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;
Valued Guide
Posts: 505

Re: call execute

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

Ask a Question
Discussion stats
  • 5 replies
  • 207 views
  • 0 likes
  • 6 in conversation