BookmarkSubscribeRSS Feed
NeehaP
Calcite | Level 5

Hi Everyone,

 

Can anybody help in deriving the below "New" variable. For each patient, first group of flags should be 1, 2nd group of flags should be 2 and so on.

 

PatFLNew
1y1
1  
1y2
1y2
1  
1y3
1y3
1y3
2  
2y1
2  
2y2

 

1 REPLY 1
ballardw
Super User

Keeping sequential values of a counter:

 

data want;
   set have;
   by notsorted pat fl;
   retain count ;
   if first.pat then count=.;
   if first.fl and fl='y' then count+1;
   if fl='y' then New=count;
   drop count;
run;

IF your data does not have all of the PAT values together you won't like the result.

At which point, good luck sorting your data to correct order unless there are a lot of other variables involved to manage such.

 

You may want to consider setting numeric flag values of 1 instead of character 'y'.

If you most common use is "if variable ='y' then <do something> you can simplify code to : if variable then <do>. SAS treats 1 as "true" in comparisons. To get number of "yes" you can just sum the flag values in any number of procedures which may be easier then counting 'y'.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 1 reply
  • 448 views
  • 0 likes
  • 2 in conversation