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

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

1 ACCEPTED SOLUTION

Accepted Solutions
RichardinOz
Quartz | Level 8

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

5 REPLIES 5
RichardinOz
Quartz | Level 8

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

Haikuo
Onyx | Level 15

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

responcity
Calcite | Level 5

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

RichardinOz
Quartz | Level 8

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

responcity
Calcite | Level 5

Great,, Thanks Richard

SOLVED :smileygrin:

Regards,

Responcity

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 5 replies
  • 501 views
  • 3 likes
  • 3 in conversation