I have a dataset(example below), with fields Scenarios, Date, Counter, Value and I need to create New Value. For each scenario, New Value takes value from latest counter(first counter from a continuous series) -
Scenarios | Date | Counter | Value | New Value |
Scn1 | Jan-22 | 0 | 189 | |
Scn1 | Feb-22 | 0 | 189 | |
Scn1 | Mar-22 | 1 | 189 | 189 |
Scn1 | Apr-22 | 1 | 189 | |
Scn1 | May-22 | 1 | 189 | |
Scn1 | Jun-22 | 0 | 0 | |
Scn2 | Jan-22 | 0 | 167 | |
Scn2 | Feb-22 | 1 | 167 | 167 |
Scn2 | Mar-22 | 1 | 167 | |
Scn2 | Apr-22 | 0 | 256 | |
Scn2 | May-22 | 1 | 256 | 256 |
Scn2 | Jun-22 | 0 | 0 | |
Scn3 | Jan-22 | 1 | 23 | 23 |
Scn3 | Feb-22 | 1 | 23 | |
Scn3 | Mar-22 | 1 | 23 | |
Scn3 | Apr-22 | 0 | 304 | |
Scn3 | May-22 | 1 | 304 | 304 |
Scn3 | Jun-22 | 1 | 304 |
I am not sure how to achieve this conditional back-casting. May I request any help on this?
Clarify how you are defining the groups?
Looks like you want to increment the grouping when counter becomes zero.
First let's convert your listing into actual data. Lets use a SINGULAR name for that first variable to make the code clearer.
data have;
input Scenario $ Date :monyy. Counter Value Expected;
format date monyy7. ;
cards;
Scn1 Jan-22 0 . 189
Scn1 Feb-22 0 . 189
Scn1 Mar-22 1 189 189
Scn1 Apr-22 1 . 189
Scn1 May-22 1 . 189
Scn1 Jun-22 0 . 0
Scn2 Jan-22 0 . 167
Scn2 Feb-22 1 167 167
Scn2 Mar-22 1 . 167
Scn2 Apr-22 0 . 256
Scn2 May-22 1 256 256
Scn2 Jun-22 0 . 0
Scn3 Jan-22 1 23 23
Scn3 Feb-22 1 . 23
Scn3 Mar-22 1 . 23
Scn3 Apr-22 0 . 304
Scn3 May-22 1 304 304
Scn3 Jun-22 1 . 304
;
So the data is actually sorted by SCENARIO and DATE, but we can use the NOTSORTED keyword on the BY statement to have SAS calculated FIRST. and LAST. flags for COUNTER within SCENARIO.
data want;
set have;
by scenario counter notsorted;
if first.scenario then group=1;
else if first.counter and counter=0 then group+1;
run;
Now we can see that your expected new variable aligns with this new grouping variable.
Obs Scenario Date Counter Value Expected group 1 Scn1 JAN2022 0 . 189 1 2 Scn1 FEB2022 0 . 189 1 3 Scn1 MAR2022 1 189 189 1 4 Scn1 APR2022 1 . 189 1 5 Scn1 MAY2022 1 . 189 1 6 Scn1 JUN2022 0 . 0 2 7 Scn2 JAN2022 0 . 167 1 8 Scn2 FEB2022 1 167 167 1 9 Scn2 MAR2022 1 . 167 1 10 Scn2 APR2022 0 . 256 2 11 Scn2 MAY2022 1 256 256 2 12 Scn2 JUN2022 0 . 0 3 13 Scn3 JAN2022 1 23 23 1 14 Scn3 FEB2022 1 . 23 1 15 Scn3 MAR2022 1 . 23 1 16 Scn3 APR2022 0 . 304 2 17 Scn3 MAY2022 1 304 304 2 18 Scn3 JUN2022 1 . 304 2
So we could use a double DOW loop to find the first non-missing value per GROUP and attach it to each observation in the GROUP.
data want;
do until(last.group);
set want;
by scenario group;
new_value=coalesce(new_value,value);
end;
new_value=coalesce(new_value,0);
do until(last.group);
set want;
by scenario group;
output;
end;
run;
Result
new_ Obs Scenario Date Counter Value Expected group value 1 Scn1 JAN2022 0 . 189 1 189 2 Scn1 FEB2022 0 . 189 1 189 3 Scn1 MAR2022 1 189 189 1 189 4 Scn1 APR2022 1 . 189 1 189 5 Scn1 MAY2022 1 . 189 1 189 6 Scn1 JUN2022 0 . 0 2 0 7 Scn2 JAN2022 0 . 167 1 167 8 Scn2 FEB2022 1 167 167 1 167 9 Scn2 MAR2022 1 . 167 1 167 10 Scn2 APR2022 0 . 256 2 256 11 Scn2 MAY2022 1 256 256 2 256 12 Scn2 JUN2022 0 . 0 3 0 13 Scn3 JAN2022 1 23 23 1 23 14 Scn3 FEB2022 1 . 23 1 23 15 Scn3 MAR2022 1 . 23 1 23 16 Scn3 APR2022 0 . 304 2 304 17 Scn3 MAY2022 1 304 304 2 304 18 Scn3 JUN2022 1 . 304 2 304
Clarify how you are defining the groups?
Looks like you want to increment the grouping when counter becomes zero.
First let's convert your listing into actual data. Lets use a SINGULAR name for that first variable to make the code clearer.
data have;
input Scenario $ Date :monyy. Counter Value Expected;
format date monyy7. ;
cards;
Scn1 Jan-22 0 . 189
Scn1 Feb-22 0 . 189
Scn1 Mar-22 1 189 189
Scn1 Apr-22 1 . 189
Scn1 May-22 1 . 189
Scn1 Jun-22 0 . 0
Scn2 Jan-22 0 . 167
Scn2 Feb-22 1 167 167
Scn2 Mar-22 1 . 167
Scn2 Apr-22 0 . 256
Scn2 May-22 1 256 256
Scn2 Jun-22 0 . 0
Scn3 Jan-22 1 23 23
Scn3 Feb-22 1 . 23
Scn3 Mar-22 1 . 23
Scn3 Apr-22 0 . 304
Scn3 May-22 1 304 304
Scn3 Jun-22 1 . 304
;
So the data is actually sorted by SCENARIO and DATE, but we can use the NOTSORTED keyword on the BY statement to have SAS calculated FIRST. and LAST. flags for COUNTER within SCENARIO.
data want;
set have;
by scenario counter notsorted;
if first.scenario then group=1;
else if first.counter and counter=0 then group+1;
run;
Now we can see that your expected new variable aligns with this new grouping variable.
Obs Scenario Date Counter Value Expected group 1 Scn1 JAN2022 0 . 189 1 2 Scn1 FEB2022 0 . 189 1 3 Scn1 MAR2022 1 189 189 1 4 Scn1 APR2022 1 . 189 1 5 Scn1 MAY2022 1 . 189 1 6 Scn1 JUN2022 0 . 0 2 7 Scn2 JAN2022 0 . 167 1 8 Scn2 FEB2022 1 167 167 1 9 Scn2 MAR2022 1 . 167 1 10 Scn2 APR2022 0 . 256 2 11 Scn2 MAY2022 1 256 256 2 12 Scn2 JUN2022 0 . 0 3 13 Scn3 JAN2022 1 23 23 1 14 Scn3 FEB2022 1 . 23 1 15 Scn3 MAR2022 1 . 23 1 16 Scn3 APR2022 0 . 304 2 17 Scn3 MAY2022 1 304 304 2 18 Scn3 JUN2022 1 . 304 2
So we could use a double DOW loop to find the first non-missing value per GROUP and attach it to each observation in the GROUP.
data want;
do until(last.group);
set want;
by scenario group;
new_value=coalesce(new_value,value);
end;
new_value=coalesce(new_value,0);
do until(last.group);
set want;
by scenario group;
output;
end;
run;
Result
new_ Obs Scenario Date Counter Value Expected group value 1 Scn1 JAN2022 0 . 189 1 189 2 Scn1 FEB2022 0 . 189 1 189 3 Scn1 MAR2022 1 189 189 1 189 4 Scn1 APR2022 1 . 189 1 189 5 Scn1 MAY2022 1 . 189 1 189 6 Scn1 JUN2022 0 . 0 2 0 7 Scn2 JAN2022 0 . 167 1 167 8 Scn2 FEB2022 1 167 167 1 167 9 Scn2 MAR2022 1 . 167 1 167 10 Scn2 APR2022 0 . 256 2 256 11 Scn2 MAY2022 1 256 256 2 256 12 Scn2 JUN2022 0 . 0 3 0 13 Scn3 JAN2022 1 23 23 1 23 14 Scn3 FEB2022 1 . 23 1 23 15 Scn3 MAR2022 1 . 23 1 23 16 Scn3 APR2022 0 . 304 2 304 17 Scn3 MAY2022 1 304 304 2 304 18 Scn3 JUN2022 1 . 304 2 304
Too many things to learn, in one single solution! Many thanks Tom.
@thepushkarsingh wrote:
May I ask one follow up please? What if Value isn't missing in other cases, is there a neater way to pick only the relevant "Value"s in to New Value?
Sure. But you have to have a rule.
Is it the MIN value? The MAX value?
The value where "counter" variable first becomes 1?
All of those a easy.
So use the same NOTSORTED trick.
data want;
do until(last.group);
set want;
by scenario group counter notsorted;
if first.counter and counter=1 then new_value=value;
end;
new_value=coalesce(new_value,0);
do until(last.group);
set want;
by scenario group;
output;
end;
run;
You might be able to do it all in one step without adding the GROUP number.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.