Flagging ever late payment in time series data

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 14
Accepted Solution

Flagging ever late payment in time series data

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


Accepted Solutions
Solution
‎04-16-2013 01:15 AM
Super Contributor
Posts: 644

Re: Flagging ever late payment in time series data

Posted in reply to 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

View solution in original post


All Replies
Solution
‎04-16-2013 01:15 AM
Super Contributor
Posts: 644

Re: Flagging ever late payment in time series data

Posted in reply to 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

Respected Advisor
Posts: 3,156

Re: Flagging ever late payment in time series data

Posted in reply to responcity

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

Occasional Contributor
Posts: 14

Re: Flagging ever late payment in time series data

Posted in reply to responcity

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

Super Contributor
Posts: 644

Re: Flagging ever late payment in time series data

Posted in reply to 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

Occasional Contributor
Posts: 14

Re: Flagging ever late payment in time series data

Posted in reply to RichardinOz

Great,, Thanks Richard

SOLVED :smileygrin:

Regards,

Responcity

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 239 views
  • 3 likes
  • 3 in conversation