Hello. Here is what I have:
PT_ID date result criteria_met
x 1/1/2015 500 yes
x 2/1/2015 470 no
x 3/1/2015 525 yes
z 2/2/2015 450 no
z 3/32015 575 yes
k 5/4/2015 650 yes
basically, what I want is all the data for a particular patient up through their first 'Yes' in the criteria_met column. So patient x will have 1 observation, patient z will have 2 and patient k will have 1 observation.
Any help is appreicated! Thank you!
Code not tested but should do the job.
data want(drop=_output_flg);
set have;
by pt_id date;
retain _output_flg;
if first.pt_id then _output_flg=1;
if _output_flg=1 then output;
if upcase(criteria_met)='YES' then _output_flg=0;
run;
Please try
data have;
input PT_ID$ date: ddmmyy10. result criteria_met$;
format date date9.;
cards;
x 1/1/2015 500 yes
x 2/1/2015 470 no
x 3/1/2015 525 yes
z 2/2/2015 450 no
z 3/3/2015 575 yes
k 5/4/2015 650 yes
;
proc sort data=have;
by pt_id descending criteria_met date;
run;
data want;
do until(first.pt_id);
set have;
by pt_id descending criteria_met date;
if first.pt_id then yesdate=date;
end;
do until(last.pt_id);
set have;
by pt_id descending criteria_met date;
if date<=yesdate then output;
format yesdate date9.;
end;
run;
You want to output all records up through the first positive (criteria_met='yes'). So all you need to do is keep a running count of the number of positives encountered (N_POS in the program below).
Notes:
data have;
input PT_ID$ date: ddmmyy10. result criteria_met$;
format date date9.;
cards;
x 1/1/2015 500 yes
x 2/1/2015 470 no
x 3/1/2015 525 yes
z 2/2/2015 450 no
z 3/3/2015 575 yes
k 5/4/2015 650 yes
;
data want;
set have;
by pt_id notsorted;
if first.pt_id then n_pos=0;
if n_pos=0;
n_pos + (criteria_met='yes');
drop n_pos;
run;
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 16. Read more here about why you should contribute and what is in it for you!
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: