BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.

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) -

ScenariosDateCounterValueNew Value
Scn1Jan-220 189
Scn1Feb-220 189
Scn1Mar-221189189
Scn1Apr-221 189
Scn1May-221 189
Scn1Jun-220 0
Scn2Jan-220 167
Scn2Feb-221167167
Scn2Mar-221 167
Scn2Apr-220 256
Scn2May-221256256
Scn2Jun-220 0
Scn3Jan-2212323
Scn3Feb-221 23
Scn3Mar-221 23
Scn3Apr-220 304
Scn3May-221304304
Scn3Jun-221 304

 

I am not sure how to achieve this conditional back-casting. May I request any help on this? 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

 

 

View solution in original post

7 REPLIES 7
Tom
Super User Tom
Super User

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

 

 

thepushkarsingh
Quartz | Level 8

Too many things to learn, in one single solution! Many thanks Tom.

thepushkarsingh
Quartz | Level 8
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?
Tom
Super User Tom
Super User

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

thepushkarsingh
Quartz | Level 8
The value where counter becomes 1 at first in the series of 1s.
Tom
Super User Tom
Super User

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.

thepushkarsingh
Quartz | Level 8
Thank you so much! You're a genius!

SAS Innovate 2025: Register Now

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!

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
  • 7 replies
  • 991 views
  • 0 likes
  • 2 in conversation