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

My data needs to look like this

id     want     yes/no

1     1          y

1     1          y

1     1          y

1     0          n

1     0          n

1     1          n

1     1          n

2     1          y

2     1          y

2     0          n

2     1          n

2     1          n  

2     0          n

2     1          n

2     1          n

basically for each id, I want "y" only for the first continuous want = 1. From the point want = 0, I want to ignore the subsequent rows for that di even if want =1 again for that id.

This is represented by "y" and "n". I need to know how to code so I get the right "y"s and "n"s. I tried do until and do while but my inexperience makes them inexecutable.

Thanks.


1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

There must be a more elegant way, but this should do it:

data want;

  set have;

  by id want notsorted;

  retain yes_no  any_1s ' ';

  if first.id then do;

     any_1s='n';

     yes_no='n';

  end;

  if first.want then do;

     if want=1 and any_1s='n' then do;

        yes_no='y';

        any_1s='y';

    end;

     else yes_no='n';

  end;

  drop any_1s;

run;

View solution in original post

6 REPLIES 6
Astounding
PROC Star

There must be a more elegant way, but this should do it:

data want;

  set have;

  by id want notsorted;

  retain yes_no  any_1s ' ';

  if first.id then do;

     any_1s='n';

     yes_no='n';

  end;

  if first.want then do;

     if want=1 and any_1s='n' then do;

        yes_no='y';

        any_1s='y';

    end;

     else yes_no='n';

  end;

  drop any_1s;

run;

ballardw
Super User

There may be some ambiguity in your requirement to "ignore". Do mean to delete the data or not execute some additional code?

You also say " so I get the right "y"s and "n"s." but you don't address which the "right" n's would be.

saurabhc
Fluorite | Level 6

Hi ballardw,

What I meant is this, keep flagging or counting or whatever, until the want changes to a zero. Once a want changes to zero, it does not matter even if it changes to a want = 1 again for that particular id. Basically, I just want the first "set" of continuous want = 1 for any given id and ignore the want = 0 and the subsequent want = 1 (in sets or single).

Ignore could mean drop the observation or just change the flag or stop counting or whatever (like above). The idea is the same.

Sorry about the ambiguity.

Thanks!

PS: All ids always start with want = 1 in my dataset.

Patrick
Opal | Level 21

Does below code give you the result you're after?

data want(drop=_:);

  set have;

  by id want notsorted;

  if first.id then _counter=0;

  if first.want and want=1 then _counter+1;

  if want=1 and _counter=1 then yn_flg='y';

  else yn_flg='n';

run;

saurabhc
Fluorite | Level 6

Hi Patrick,

Thank you. Your code works too! It is nice to know how different lines produce the same result because they make different use of similar logic.

Thanks again.

saurabhc
Fluorite | Level 6

Hi Astounding,

The code you gave totally works after adjusting it to my actual data. Basically there were also multiple dates with the ids but they did not matter as long as my indicator "want" had preselected my desirable observations.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 6 replies
  • 1305 views
  • 3 likes
  • 4 in conversation