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

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;

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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;

View solution in original post

6 REPLIES 6
Tom
Super User Tom
Super User

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;
monday89
Fluorite | Level 6

Thanks! The second box worked! 

ballardw
Super User

Where do those values come from and why do you think that they need to be in macro variables?

PaigeMiller
Diamond | Level 26

@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.

--
Paige Miller
Kurt_Bremser
Super User

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.

kelxxx
Quartz | Level 8
%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;

 

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 1270 views
  • 0 likes
  • 6 in conversation