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

Hi, 

I'm working with a dataset that looks something like below:

accountmonth_endactive_stat
131-Jan-20yes
129-Feb-20yes
131-Mar-20yes
231-Jan-20yes
229-Feb-20no
231-Mar-20no
331-Jan-20no
329-Feb-20no
331-Mar-20yes

 

Account field: unique account number

Month_end: date

active_stat: indicator showing whether an account is active or not during that month

 

I'd like to get help with creating a new column that returns active status of the accounts in the data sets in previous month. Below is example of the output i'd like to achieve, where prevmon_stat shows active status of the account in previous month.

 

accountmonth_endactive_statprevmon_stat
131-Jan-20yes 
129-Feb-20yesyes
131-Mar-20yesyes
231-Jan-20yes 
229-Feb-20noyes
231-Mar-20nono
331-Jan-20no 
329-Feb-20nono
331-Mar-20yesno

 

Thank you for your help!

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

You could use the LAG() function, which returns the value from the previous time it executes.  

data want;
  set have;
  by account;
  prevmon_stat=lag(active_stat);
  if first.account then call missing(prevmon_stat);
run;

Note that it knows nothing about months. So if your data is missing a month or has a two observations for the same month then the prevmon_stat value is not from the previous month. It is just the value that it remembered from the previous time it executed.

View solution in original post

2 REPLIES 2
Tom
Super User Tom
Super User

You could use the LAG() function, which returns the value from the previous time it executes.  

data want;
  set have;
  by account;
  prevmon_stat=lag(active_stat);
  if first.account then call missing(prevmon_stat);
run;

Note that it knows nothing about months. So if your data is missing a month or has a two observations for the same month then the prevmon_stat value is not from the previous month. It is just the value that it remembered from the previous time it executed.

Tony_N
Calcite | Level 5
Hi Tom,

It works! Thank you so much for your quick solution

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!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 779 views
  • 1 like
  • 2 in conversation