BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
PetePatel
Quartz | Level 8

Hi,

 

I have a large dataset that I would like to transform to ID level using the entire history. I am unsure on the most efficient way to do this but it's probably better explaining using the data below.

 

I am only interested in capturing where flag is populated (flag=1).

 

IDMonFlagAmt
1Jan-18 100
1Feb-18 200
1Mar-18 250
1Apr-181400
1May-18 300
2Jan-15 50
2Feb-15 75
2Mar-151125
2Apr-15 300
2May-15 500
3Dec-14 600
3Jan-15 700
3Feb-15 650
3Mar-15 400
3Apr-15 300
3May-151100
3Jun-15 0
4Jan-18 500
4Feb-18 600
4Mar-18 700
4Apr-18 800

 

I want to transform to:

 

IDAmtMon_flag
1250Apr-18
275Mar-15
3300May-15

 

The 'Amt' is taken from the previous month prior to the flag=1 event.

'Mon_flag' is the month the flag was triggered.

 

I did start by selecting where flag=1 and then merging back into the history by previous month but it took a long time!

 

What is the most efficient way to carry this out in a reduced number of steps?

 

Thanks in advance.

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

Try this

 

data have;
input ID Mon $ Flag Amt;
infile datalines dsd dlm=',';
datalines;
1,Jan-18,,100
1,Feb-18,,200
1,Mar-18,,250
1,Apr-18,1,400
1,May-18,,300
2,Jan-15,,50
2,Feb-15,,75
2,Mar-15,1,125
2,Apr-15,,300
2,May-15,,500
3,Dec-14,,600
3,Jan-15,,700
3,Feb-15,,650
3,Mar-15,,400
3,Apr-15,,300
3,May-15,1,100
3,Jun-15,,0
4,Jan-18,,500
4,Feb-18,,600
4,Mar-18,,700
4,Apr-18,,800
;

data want(keep=Id Amt Mon_flag);
    set have;
    by ID;
    lagAmt = lag1(amt);
    if not first.ID & flag=1 then do;
        amt=lagAmt;
        output;
    end;
    rename Mon=Mon_flag;
run;

View solution in original post

3 REPLIES 3
PeterClemmensen
Tourmaline | Level 20

Try this

 

data have;
input ID Mon $ Flag Amt;
infile datalines dsd dlm=',';
datalines;
1,Jan-18,,100
1,Feb-18,,200
1,Mar-18,,250
1,Apr-18,1,400
1,May-18,,300
2,Jan-15,,50
2,Feb-15,,75
2,Mar-15,1,125
2,Apr-15,,300
2,May-15,,500
3,Dec-14,,600
3,Jan-15,,700
3,Feb-15,,650
3,Mar-15,,400
3,Apr-15,,300
3,May-15,1,100
3,Jun-15,,0
4,Jan-18,,500
4,Feb-18,,600
4,Mar-18,,700
4,Apr-18,,800
;

data want(keep=Id Amt Mon_flag);
    set have;
    by ID;
    lagAmt = lag1(amt);
    if not first.ID & flag=1 then do;
        amt=lagAmt;
        output;
    end;
    rename Mon=Mon_flag;
run;
PetePatel
Quartz | Level 8

Thanks for the quick reply Draycut, this is so much more cleaner efficient.

PeterClemmensen
Tourmaline | Level 20

Anytime, glad you found your answer 🙂

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 3 replies
  • 1019 views
  • 2 likes
  • 2 in conversation