Hello,
I created a macro to calculate two values based on input and assign them to 2 global macro variables _obs and _rate. But right now I have some problems when use it in a data step.
My data step code is the following:
data d2;
set d1;
call execute('%nrstr(%incidence_sys(_var1="'||Type||'"))');
obs = &_obs;
rate = &_rate;
run;
The macro is %incidence_sys. I want to use each 'Type' of observation as my macro input. Then the macro computes and saves those two global macro variables: _obs and _rate. Next I need to assign them to two variables of each row in dataset. But the code seems run the macro through all rows first then assigns the last _obs and _rate back to all rows. I set my macro to put some results, it did calculate values based on each row.
My data set is like
ID Type ...
1 A
2 B
3 B
4 C
I expect the result is like
ID Type obs rate...
1 A 10 0.11
2 B 12 0.51
3 B 11 0.41
4 C 5 0.3
But currently my result is
ID Type obs rate...
1 A 5 0.3
2 B 5 0.3
3 B 5 0.3
4 C 5 0.3
Maybe you want function DOSUBL().
%macro xx(var1=);
%global a ;
%if &var1=A %then %let a=10;
%else %if &var1=B %then %let a=12;
%else %if &var1=C %then %let a=18;
%mend;
data have;
input ID Type$;
cards;
1 A
2 B
3 B
4 C
;
run;
data want;
set have;
rc=dosubl(cats('%xx(var1=',type,')'));
rate=symgetn('a');
run;
Order of operations.
Call Execute executes AFTER the data step is finished, so the variables don't exist.
Is PROC FCMP an option instead, since it can return values, like a function?
Thanks, Reeza.
My macro does return values. But it includes some SQL procedures.
One problem is that CALL EXECUTE doesn't do anything until a step boundary, in other words it doesn't set values in the present data step or make macro variables available in the present data step.
Of course, there may be other problems as well since we can't see your data and we don't know what %incidence_sys does.
To embellish upon what @PaigeMiller said, you can't run PROC SQL in the middle of a DATA step. It doesn't matter whether you hard-code the PROC SQL, generate it with macro language, or use CALL EXECUTE. PROC SQL cannot be embedded within a DATA step.
You have already provided some sample inputs. If you were to describe your intentions -- how the inputs should be used to calculate the outputs -- there are probably other ways to get there.
Macro variables are resolved before the data step is compiled (and then run), while code included in call execute executes after the current step has finished, so if you expect the macro incidence_sys to set &obs and &rate, they're actually two execution stages apart.
Maybe you want function DOSUBL().
%macro xx(var1=);
%global a ;
%if &var1=A %then %let a=10;
%else %if &var1=B %then %let a=12;
%else %if &var1=C %then %let a=18;
%mend;
data have;
input ID Type$;
cards;
1 A
2 B
3 B
4 C
;
run;
data want;
set have;
rc=dosubl(cats('%xx(var1=',type,')'));
rate=symgetn('a');
run;
Thanks Ksharp.
It works and gives me what I want exactly.
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.