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

Hi, i need to create new column previous_date for each acct_no, if current row is the first observation for particular acct_no then previous_date should be null, otherwise it should be the 

observation(eff_date) from previous row, on the screenshot of results on previous_date, the value should be 21Jul2023 on row3 instead of null and row4 should be 30Jun2023         

 

results.PNG 

data have;
input ACCT_NO EFF_DATE : date9. ;
format EFF_DATE date9.;
datalines;
1 21JUL2023
1 25JUL2023
2 30JUN2023
2 10JUL2023
2 01AUG2023
;
run;


proc sort data=have;
by ACCT_NO EFF_DATE;
run;


data want;
set have;
by ACCT_NO;

if first.ACCT_NO then previous_date = .;

else previous_date = lag(EFF_DATE);

format previous_date date9.;

run;


1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26
data have;
input ACCT_NO EFF_DATE : date9. ;
format EFF_DATE date9.;
datalines;
1 21JUL2023
1 25JUL2023
2 30JUN2023
2 10JUL2023
2 01AUG2023
;
data want;
    set have;
    by acct_no;
    lag_date=lag(eff_date);
    if first.acct_no then previous_date=.;
    else previous_date=lag_date;
    drop lag_date;
    format previous_date date9.;
run;
--
Paige Miller

View solution in original post

3 REPLIES 3
PaigeMiller
Diamond | Level 26
data have;
input ACCT_NO EFF_DATE : date9. ;
format EFF_DATE date9.;
datalines;
1 21JUL2023
1 25JUL2023
2 30JUN2023
2 10JUL2023
2 01AUG2023
;
data want;
    set have;
    by acct_no;
    lag_date=lag(eff_date);
    if first.acct_no then previous_date=.;
    else previous_date=lag_date;
    drop lag_date;
    format previous_date date9.;
run;
--
Paige Miller
mkeintz
PROC Star

The lag function is NOT a "lookback".  It is a queue management function.  What you need is to always update the queue, but not always keep the results.

 

The IFN function lets you do this, as in:

 

data have;
input ACCT_NO EFF_DATE : date9. ;
format EFF_DATE date9.;
datalines;
1 21JUL2023
1 25JUL2023
2 30JUN2023
2 10JUL2023
2 01AUG2023
;
data want;
    set have;
    by acct_no;
    previous_date=ifn(first.acct_no,.,lag(eff_date));
    format previous_date date9.;
run;

The advantage of the IFN function is that both results (the 2nd and 3rd arguments) are executed, and then one of them is chosed based in the first argument.  So the lag is always updated, but not always returned to previous_date.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Solly7
Pyrite | Level 9
thank you for clarity and noted

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 321 views
  • 2 likes
  • 3 in conversation