I have a table that is being created, we will say that column 1 is YearMonth and column2 as Flag.
YearMonth Flag 200101 1 200102 1 200103 0 200104 1 200105 1 200106 0 200107 1 200108 0
Note: First entry of flag column will always be 1.
I want to add 1 to flag if current flag doesn't match with previous row value along with one major condition (explained after output for a better explanation).
The output should be:
YearMonth Flag Stage 200101 1 1 200102 1 1 200103 0 2 200104 1 3 200105 1 3 200106 0 4 200107 1 3 200108 0 4
Please note there are only 4 stages. Hence if a flag is repeated after stage 4, then it should not increment and should give output as either stage=3 if flag=1 or stage=4 if flag=0.
I am trying something like this:
data one;
set Query;
Stage=lag(flag);
if first.flag then Stage=1;
if first.flag then Stage=1;
if flag ne Stage+1 then Stage=Stage+1;
run;
An explanation of why this code isnt working would be really helpful. Thank you!
Also, I am aware that I am not doing something once it reaches stage 4.
This is essentially counting groups of observations. So use BY group processing. Add the NOTSORTED keyword in the BY statement so SAS doesn't complain that the values aren't sorted. Increment the counter when starting a new group.
data want;
set have;
by flag notsorted;
stage + first.flag;
run;
To add your second criteria you could just add this line
stage = min(stage,4 - flag);