DATA Step, Macro, Functions and more

selecting the series of observation based on some condition

Accepted Solution Solved
Reply
Contributor ved
Contributor
Posts: 27
Accepted Solution

selecting the series of observation based on some condition

Hi guys,

 

i have a data problem where i have to select out group of observation until certian condition is met ( until i have find flag=1). here is the sample datset:

 

data have;
    input id flag;
cards;
001 0
001 0
001 0
001 1
001 0
001 0
002 0
002 1
002 0
;
run;

data want;
    input id flag;
cards;
001 0
001 0
001 0
001 1
002 0
002 1
;
run;

 

i was trying to use do loop , but seems like i am doing something wrong:

here is my try:

data want;
do until(last.id);
    set have;
    by id;    
    do _n_=1 to (f<=1);
        if f<=1;
    end;
    output;
    end;
stop;
run;   

 

but it's not working. Any help is highly appreciated


Accepted Solutions
Solution
‎06-01-2016 03:10 AM
SAS Super FREQ
Posts: 684

Re: selecting the series of observation based on some condition

Hi

 

LAST.ID will be set to 1 as soon as the end of the first group is reached, so this is why, you only see one group.

 

Find below a code sample, that will do what you need. It is based on the SUM statement.

 

data want2;
  set have;
  by id;

  if first.id = 1 then do;
    flagCount = 0;
  end;
  flagCount + (flagCount + flag);

  if flagCount <= 1;
run;

Bruno

View solution in original post


All Replies
Super User
Super User
Posts: 7,413

Re: selecting the series of observation based on some condition

Hi,

 

Something like:

data want (drop=op);
  set have;
  by id;
  retain op;
  if first.id then op=1;
  if op=1 then output;
  if flag=1 then op=0;
run;
Solution
‎06-01-2016 03:10 AM
SAS Super FREQ
Posts: 684

Re: selecting the series of observation based on some condition

Hi

 

LAST.ID will be set to 1 as soon as the end of the first group is reached, so this is why, you only see one group.

 

Find below a code sample, that will do what you need. It is based on the SUM statement.

 

data want2;
  set have;
  by id;

  if first.id = 1 then do;
    flagCount = 0;
  end;
  flagCount + (flagCount + flag);

  if flagCount <= 1;
run;

Bruno

Contributor ved
Contributor
Posts: 27

Re: selecting the series of observation based on some condition

@Bruno_SAS Hi Bruno, Thanks for the solution, but what if i need like this

 

data have;
    infile cards missover;
    input id flag switch $8.;
cards;
001 0
001 0
001 0
001 1 first
001 0
001 0
001 1 second
001 0
001 0
002 0
002 1 first
002 0
002 0
002 1 second
002 1 third
;
run;
 
/*I need three outputs based on switches i,e.*/
/**/
/*data first will looks like:*/

data first;
    input id flag switch $8.;
cards;
001 0
001 0
001 0
001 1 first
002 0
002 1 first
;
run;

/*this is selecting observation from first to second*/
data second;
    input id flag switch $8.;
cards;
001 1 first
001 0
001 0
001 1 second
002 1 first
002 0
002 0
002 1 second
;
run;

data third;
    input id flag switch $8.;
cards;
002 1 second
002 1 third
;
run;
   

please help!

SAS Super FREQ
Posts: 684

Re: selecting the series of observation based on some condition

hi

 

You need to change the logic for this line

flagCount + (flagCount + flag);

so that you get the right setting.

For instance

flagCount + (flagCount + (switch = "first"));

will only write the observation til you reach switch = "first"

 

Bruno

Trusted Advisor
Posts: 1,115

Re: selecting the series of observation based on some condition

@ved: A DOW loop approach (do until(last. ...); set ...) was suggested for a very similar question in this recent thread.

☑ This topic is solved.

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

Discussion stats
  • 5 replies
  • 330 views
  • 1 like
  • 4 in conversation