BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Lochen
Calcite | Level 5

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

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

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

7 REPLIES 7
Reeza
Super User

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?

 

Lochen
Calcite | Level 5

Thanks, Reeza.

 

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

PaigeMiller
Diamond | Level 26

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
Astounding
PROC Star

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. 

Kurt_Bremser
Super User

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.

Ksharp
Super User

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;
Lochen
Calcite | Level 5

Thanks Ksharp.

It works and gives me what I want exactly.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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