i have the following macro
%let group_1=(n=4);
%let group_2=(n=10);
I have the following dataset. What I could like to do is that if group_strata = 1 then the denominator = 4 from macro above and some with group_strata = 2
medication | Group_strata | numerator |
AAA | 1 | 3 |
BBB | 1 | 2 |
CCC | 1 | 1 |
AAA | 2 | 2 |
BBB | 2 | 4 |
CCC | 2 | 1 |
I tried the following and it doesn't work
data want; set have;
if group_strata = 1 then total_denominator = (&group_1);
else if group_strata = 2 then total_denominator = (&group_2);
run;
If your dataset does not have a variable named N then running this code:
data want; set have;
if group_strata = 1 then total_denominator = ((n=4));
else if group_strata = 2 then total_denominator = ((n=10));
run;
Will result in TOTAL_DENOMINATOR always being set to 0. SAS will create a new variable named N and set it to missing and missing is not equal to either 4 or 10.
Can you change how those macro variables are created?
If not then remove the N= part. For example by using %SCAN() function.
data want; set have;
if group_strata = 1 then total_denominator = %scan(&group_1,2,(=)) ;
else if group_strata = 2 then total_denominator = %scan(&group_2,2,(=)) ;
run;
If your dataset does not have a variable named N then running this code:
data want; set have;
if group_strata = 1 then total_denominator = ((n=4));
else if group_strata = 2 then total_denominator = ((n=10));
run;
Will result in TOTAL_DENOMINATOR always being set to 0. SAS will create a new variable named N and set it to missing and missing is not equal to either 4 or 10.
Can you change how those macro variables are created?
If not then remove the N= part. For example by using %SCAN() function.
data want; set have;
if group_strata = 1 then total_denominator = %scan(&group_1,2,(=)) ;
else if group_strata = 2 then total_denominator = %scan(&group_2,2,(=)) ;
run;
Thanks! The second box worked!
Where do those values come from and why do you think that they need to be in macro variables?
@ballardw wrote:
Where do those values come from and why do you think that they need to be in macro variables?
Agreed. Unless you have no control over the contents of the macro variable ... then %let group_1=(n=4); is a poor way to set things up; and its hard to see why macro variables are needed anyway.
You want to have an informat:
proc format;
invalue denom
1 = 4
2 = 10
other = .
;
run;
data want;
set have;
total_denominator = input(group_strata,denom.);
run;
Untested, for lack of usable example data.
%let group_1=(n=4); %let group_2=(n=10); data have; input medication $3 group_strata numerator; cards; AAA 1 3 BBB 1 2 CCC 1 1 AAA 2 2 BBB 2 4 CCC 2 1 ; run; data want; set have; total_denominator=input(scan(symget(cats("group_",group_strata)),2,"(=)"),8.);
run;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.