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.
Pat | FL | New |
1 | y | 1 |
1 | ||
1 | y | 2 |
1 | y | 2 |
1 | ||
1 | y | 3 |
1 | y | 3 |
1 | y | 3 |
2 | ||
2 | y | 1 |
2 | ||
2 | y | 2 |
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'.
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.