DATA Step, Macro, Functions and more

Assign the macro result of each obs to a new variable in datastep

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 8
Accepted Solution

Assign the macro result of each obs to a new variable in datastep

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

 

 


Accepted Solutions
Solution
‎10-06-2017 05:19 PM
Super User
Posts: 10,691

Re: Assign the macro result of each obs to a new variable in datastep

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;

View solution in original post


All Replies
Super User
Posts: 23,320

Re: Assign the macro result of each obs to a new variable in datastep

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?

 

Occasional Contributor
Posts: 8

Re: Assign the macro result of each obs to a new variable in datastep

Thanks, Reeza.

 

My macro does return values. But it includes some SQL procedures.

Respected Advisor
Posts: 2,833

Re: Assign the macro result of each obs to a new variable in datastep

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.

 

 

--
Paige Miller
Super User
Posts: 6,637

Re: Assign the macro result of each obs to a new variable in datastep

Posted in reply to PaigeMiller

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. 

Super User
Posts: 9,919

Re: Assign the macro result of each obs to a new variable in datastep

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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Solution
‎10-06-2017 05:19 PM
Super User
Posts: 10,691

Re: Assign the macro result of each obs to a new variable in datastep

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;
Occasional Contributor
Posts: 8

Re: Assign the macro result of each obs to a new variable in datastep

Thanks Ksharp.

It works and gives me what I want exactly.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 177 views
  • 0 likes
  • 6 in conversation