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

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 1 reply
  • 628 views
  • 0 likes
  • 2 in conversation