Hi All,
I am trying to create a flag with the following rules at ID States Level. The Dataset has ID, States, Gap(Between consecutive states) and a condition flag.
Every time the for a given ID the Condition 1 = 1 (This is universally true)
The Flag as well inititates as Type1, then if for the next state for an ID the gap is >=180 then flag = Type2, else Type1 reatins to types 2.
For states 3 and above, if the Condition 1 = 1 then reset to type and apply the above rule, else retain earlier flag.
Below is a sample datset...
ID States Gap Condition 1 Flag 1087013 1 0 1 Type1 1087013 2 180 0 Type2 1087013 3 34 0 Type2 1087013 4 45 1 Type1 1087013 5 77 0 Type1 1103719 1 0 1 Type1 1103719 2 743 0 Type2 1103719 3 20 0 Type2 1103719 4 2 0 Type2 1103719 5 0 1 Type1 1103719 6 50 0 Type1 1103719 7 10 0 Type1 1103719 8 0 0 Type1 1126597 1 0 1 Type1 1126597 2 200 0 Type2 1126597 3 30 0 Type2 1126597 4 0 0 Type2 1293105 1 0 1 Type1 1297440 1 0 1 Type1
Any help is appreciated!!
Thanks in Advance
Retain flag;
if condition=1 then flag = 'Type1';
else if gap>=180 then flag = 'Type2';
This assumes you DO NOT have a variable named flag in your dataset already.
@SambitNandi wrote:
That is what I need to create.
Did the code not work?
I think a little more needs to be built into the logic:
data want;
set have;
by ID;
if first.ID then flag='Type1';
else if gap >= 180 then flag='Type2';
if states >= 3 and condition_1=1 then flag='Type1';
retain flag;
run;
It's not 100% clear whether it is possible to have gap >= 180 for the first observation for a STATE. What should happen in that case?
I can't test it today, so you'll need to try it to see if it gives you any trouble.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.