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

Hi All,

 

I want records that are 0 before 1 and should not delete 0 after 1, how can I proceed?

Please check below data.

sub dt       
1 3/8/2022 0
1 3/9/2022 0
1 3/10/2022 1
1 3/11/2022 1
1 3/12/2022 0

 

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

I think this is what you want. 

 

I added an extra sub to make it suitable for by group processing.

 

data have;
input sub dt :mmddyy10. dummy;
format dt mmddyy10.;
datalines; 
1 3/8/2022  0
1 3/9/2022  0
1 3/10/2022 1
1 3/11/2022 1
1 3/12/2022 0
2 3/8/2022  0
2 3/9/2022  0
2 3/10/2022 1
2 3/11/2022 1
2 3/12/2022 0
;

data want;
   set have;
   by sub;
   if first.sub then _iorc_ = 0;
   if dummy then _iorc_ = 1;
   if _iorc_;
run;

 

Result

 

sub  dt          dummy
1    03/10/2022  1 
1    03/11/2022  1 
1    03/12/2022  0 
2    03/10/2022  1 
2    03/11/2022  1 
2    03/12/2022  0 

View solution in original post

6 REPLIES 6
PeterClemmensen
Tourmaline | Level 20

Do you want to keep or delete records with 0 before 1?

 

Do you want this logic to work by ID (sub) ?

KalaBhairava
Quartz | Level 8

either way, delete or flagging both are fine

PeterClemmensen
Tourmaline | Level 20

I think this is what you want. 

 

I added an extra sub to make it suitable for by group processing.

 

data have;
input sub dt :mmddyy10. dummy;
format dt mmddyy10.;
datalines; 
1 3/8/2022  0
1 3/9/2022  0
1 3/10/2022 1
1 3/11/2022 1
1 3/12/2022 0
2 3/8/2022  0
2 3/9/2022  0
2 3/10/2022 1
2 3/11/2022 1
2 3/12/2022 0
;

data want;
   set have;
   by sub;
   if first.sub then _iorc_ = 0;
   if dummy then _iorc_ = 1;
   if _iorc_;
run;

 

Result

 

sub  dt          dummy
1    03/10/2022  1 
1    03/11/2022  1 
1    03/12/2022  0 
2    03/10/2022  1 
2    03/11/2022  1 
2    03/12/2022  0 
KalaBhairava
Quartz | Level 8

Thank you very much and it's working well.

KalaBhairava
Quartz | Level 8
Can you give me code to flags those records with 0 before 1?

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 2070 views
  • 1 like
  • 2 in conversation