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

Hi,

Can someone help me please?

I have the following data set and would like to count the number of iterations on 'outcome' before values THIRDPARTY and RIGHTPARTY are true, i.e. on patid 1 (when sorted by date) the value of THIRDPARTY is met on the very first record so the count will be 1. I'd like to retain this value even though the condition is true on record #5 for the same ID. On padtid 2 the count will be 4 .. the idea is to retain the very first instance of either THIRDPARTY or RIGHTPARTY.

Thanks in advance!

data dsn;

input patid calldate date9. outcome $15. ;

datalines;

1 01JAN2012 THIRDPARTY

1 02JAN2012 NO

1 03JAN2012 NO

1 04JAN2012 THIRDPARTY

2 01JAN2012 NO

2 02JAN2012 NO

2 03JAN2012 NO

3 01JAN2012 NO

3 02JAN2012 THIRDPARTY

4 02JAN2012 THIRDPARTY

4 03JAN2012 NO

2 04JAN2012 RIGHTPARTY

2 05JAN2012 RIGHTPARTY

1 02JAN2012 NO

5 01JAN2012 NO

5 02JAN2012 NO

;

run;

data dsn2;

set dsn;

format calldate date9.;

run;

proc sort data = dsn2;

by patid calldate;

run;

















1 ACCEPTED SOLUTION

Accepted Solutions
MikeZdeb
Rhodochrosite | Level 12

hi ... one way ...

data dsn;

input patid calldate :date9. outcome :$15. ;

format calldate date9.;

datalines;

1 01JAN2012 THIRDPARTY

1 02JAN2012 NO

1 03JAN2012 NO

1 04JAN2012 THIRDPARTY

2 01JAN2012 NO

2 02JAN2012 NO

2 03JAN2012 NO

3 01JAN2012 NO

3 02JAN2012 THIRDPARTY

4 02JAN2012 THIRDPARTY

4 03JAN2012 NO

2 04JAN2012 RIGHTPARTY

2 05JAN2012 RIGHTPARTY

1 02JAN2012 NO

5 01JAN2012 NO

5 02JAN2012 NO

;

proc sort data=dsn;

by patid calldate;

run;


data want (keep=patid calldate when);

do j=1 by 1 until (last.patid);

   set dsn;

   by patid;

   if missing(when) and find(outcome,'PARTY') then when = j;

end;

if missing(when) then call missing(calldate);

run;

View solution in original post

4 REPLIES 4
Tom
Super User Tom
Super User

Here is one way using the programming pattern known as a double DOW loop instead of the RETAIN function.

The variable COUNT will have the value you wanted.  The variable OBS will end up with the number of observations for that PATID.  If you do not need it then you can drop it.

data dsn;

  input patid calldate date9. outcome $15. ;

  format calldate date9.;

datalines;

1 01JAN2012 THIRDPARTY

1 02JAN2012 NO

1 03JAN2012 NO

1 04JAN2012 THIRDPARTY

2 01JAN2012 NO

2 02JAN2012 NO

2 03JAN2012 NO

3 01JAN2012 NO

3 02JAN2012 THIRDPARTY

4 02JAN2012 THIRDPARTY

4 03JAN2012 NO

2 04JAN2012 RIGHTPARTY

2 05JAN2012 RIGHTPARTY

1 02JAN2012 NO

5 01JAN2012 NO

5 02JAN2012 NO

run;


proc sort;

  by patid calldate;

run;


data want ;

  do obs=1 by 1 until(last.patid);

    set dsn ;

    by patid calldate;

    if count=. and outcome ne 'NO' then count=obs;

  end;

  do until(last.patid);

    set dsn;

    by patid calldate;

    output;

  end;

run;

bob2012
Calcite | Level 5

Thanks Tom!!

This works and outputs the records I need!

Appreciate the swift response!!

MikeZdeb
Rhodochrosite | Level 12

hi ... one way ...

data dsn;

input patid calldate :date9. outcome :$15. ;

format calldate date9.;

datalines;

1 01JAN2012 THIRDPARTY

1 02JAN2012 NO

1 03JAN2012 NO

1 04JAN2012 THIRDPARTY

2 01JAN2012 NO

2 02JAN2012 NO

2 03JAN2012 NO

3 01JAN2012 NO

3 02JAN2012 THIRDPARTY

4 02JAN2012 THIRDPARTY

4 03JAN2012 NO

2 04JAN2012 RIGHTPARTY

2 05JAN2012 RIGHTPARTY

1 02JAN2012 NO

5 01JAN2012 NO

5 02JAN2012 NO

;

proc sort data=dsn;

by patid calldate;

run;


data want (keep=patid calldate when);

do j=1 by 1 until (last.patid);

   set dsn;

   by patid;

   if missing(when) and find(outcome,'PARTY') then when = j;

end;

if missing(when) then call missing(calldate);

run;

bob2012
Calcite | Level 5

Thanks MikeZdeb! Perfect, exactly what I needed.

Thank you for responsding so quickly! Very helpful!

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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