Dear SAS Masters,
Iam a newbie using SAS, please help me to create flagging to some id who make late payment, once he late he flag forever, please kindly find the example below :
data have;
informat dclrdt yymmdd8.;
format dclrdt date9.;
input idno dclrdt flag;
cards;
101 20070131 0
101 20070228 0
101 20070331 0
101 20070430 0
101 20070531 1
101 20070630 0
101 20070731 0
101 20070831 0
101 20070930 0
101 20071031 1
101 20071130 0
101 20071231 0
101 20080131 0
202 20070131 0
202 20070228 1
202 20070331 0
202 20070430 0
202 20070531 0
202 20070630 1
202 20070731 1
202 20070831 1
202 20070930 0
202 20071031 1
202 20071130 0
202 20071231 0
202 20080131 0
;
run;
From that data i want to flag after he late till forever so the data seem like below :
data want;
informat dclrdt yymmdd8.;
format dclrdt date9.;
input idno dclrdt flag everlate;
cards;
101 20070131 0 0
101 20070228 0 0
101 20070331 0 0
101 20070430 0 0
101 20070531 1 1
101 20070630 0 1
101 20070731 0 1
101 20070831 0 1
101 20070930 0 1
101 20071031 1 1
101 20071130 0 1
101 20071231 0 1
101 20080131 0 1
202 20070131 0 0
202 20070228 1 1
202 20070331 0 1
202 20070430 0 1
202 20070531 0 1
202 20070630 1 1
202 20070731 1 1
202 20070831 1 1
202 20070930 0 1
202 20071031 1 1
202 20071130 0 1
202 20071231 0 1
202 20080131 0 1
;run;
thanks before.
Regards,
Responcity
Add the following code to your datastep ;
retain everlate ;
previd = lag(idno) ;
if idno NE previd
then everlate = 0 ;
if flag = 1 then everlate = 1 ;
everlate will only be reset to 0 when a new idno is encountered
Richard
Add the following code to your datastep ;
retain everlate ;
previd = lag(idno) ;
if idno NE previd
then everlate = 0 ;
if flag = 1 then everlate = 1 ;
everlate will only be reset to 0 when a new idno is encountered
Richard
Similar idea as Richard, while using by variable to identify id changes:
data want;
set have;
by idno notsorted;
retain everlate;
everlate=ifn(everlate=1,1,flag);
everlate=ifn(first.idno, flag, everlate);
run;
Haikuo
You're all genius man, thanks for helping :smileygrin: both of above work very well,
but im still confuse of 'retain' what it is and when to use it?
regards,
responcity
Retain changes the stock behaviour in a SAS data step: namely that before each new record is read, all variables except ones like _N_ are set to missing. So retain for the variable retains the value from the previous record, ie makes it permanent unless changed by the logic of the data step.
Lag () in my suggestion has a similar effect, but stores the previous value in another variable.
I used Lag instead of using the By ... notsorted clause because I envisaged you adding the logic to your first data step.
Richard
Great,, Thanks Richard
SOLVED :smileygrin:
Regards,
Responcity
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.