sas - Why does using CALL EXECUTE to run a macro error when running it directly works? -


i inherited sas program looks this:

%macro complicatedstuff( groupid= );      %let fileid = %sysfunc( open( work.bigdataset ) );      %put    'doing difficult ' &groupid.;      %let closerc = %sysfunc( close( &fileid. ) );  %mend complicatedstuff;  %complicatedstuff(groupid=abc1); %complicatedstuff(groupid=def2); %complicatedstuff(groupid=3ghi); %complicatedstuff(groupid=j4ki); 

being multi-faceted programmer, looked @ , thought "surely can make least little bit more dynamic". sure enough, able develop thought simple solution using call execute:

data work.ids;      input   id      $4.             ;  datalines; abc1 def2 3ghi j4ki run;  data work.commanddebug;     set work.ids;      command = cats(                 '%complicatedstuff(groupid=', id, ');'               );      call execute( command );  run; 

i happy solution until came time ftp files generated complicatedstuff different server. our sas server running on unix , sas admins created helpful little macro call named %sas_sftp (because, i've been told, x code gets ugly). unfortunately, cannot post %sas_sftp code - belongs company , don't think want on so.

i attempted call %sas_sftp macro had called %complicatedstuff macro (both second call execute within same data step, , second data step), first file (of 30) make destination. when looked @ log, looked second macro started executing before ftp had finished (the ftp pipe, or whatever is, hadn't been released before next ftp started), subsequent ftps silently failing due resource unavailability (i presume).

i thought execute queue macro calls , execute them if sequentially located in code (as were) - 1 @ time. else going on because, while first approach above worked without issue, dynamic solution failed. poured on call execute: how , why , sas documentation, i'm afraid don't understand talking about.

i did find work around (or rather, colleague found one) posted below "answer", i'd explain execute function , how works.

why didn't first attempt, using call execute, work?

call execute works code in community wiki, except specific issues relating timing. common issue run when i'm doing macro includes defines macro variable, such proc sql select into inside macro creates macro text used in macro - not dissimilar answer. because of timing rules, isn't executed until after call execute finishes constructing code executed, means value isn't changed inside code.

here example.

%macro mymacro(age=0); proc sql noprint; select quote(name) :namelist separated ',' sashelp.class age=&age.; quit;  data name_age; set sashelp.class; name in (&namelist.); run; proc print data=name_age; var name age; run; %mend mymacro;  proc sort data=sashelp.class out=class nodupkey; age; run; 

ok, have control dataset (class) , macro run off it. here call execute. not work properly; first time runs messages &namelist being undefined, second , future times age=16 (the last age) since macro variable defined as.

data _null_; set class; exec_val = cats('%mymacro(age=',age,')'); call execute(exec_val); run; 

here sql macro call. works expected.

proc sql noprint; select cats('%mymacro(age=',age,')') :calllist separated ' '     class; quit;    &calllist; 

i don't find call execute useful proc sql macro list solution data generated code, except when it's easier construct code in data step , i'm not doing causes timing issues.


Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -