Hi all.
I have a dataset containing several observations for each study participant. In each row, i have a variable indicating whether the blood pressure is within a target. I also have a varible indicating whether blood pressure is above the target.
For study participants whose blood pressure remain within the target all observation (e.g. the participant have no observation with indication of the blood pressure being above), i want to know how many times the participant have values within the target and at what date the patient was in the target for the first time.
The date is in the format date9.
data have;
input id date target above_target;
datalines;
1 24MAY2015 0 0
1 25MAY2015 0 1
1 26MAY2015 1 0
2 01JAN2016 1 0
2 02JAN2016 0 0
2 03JAN2016 1 0
3 27MAR2015 0 0
3 28MAR2015 0 0
4 26JUN2016 0 1
4 27JUN206 0 1
;
data want;
input id date_first_target number_target;
datalines;
1 . . 0
2 01JAN2016 2
3 . . 0
4 . . 0
;
I hope you can help me, thanks 🙂
PROC SUMMARY is the simplest (and generally fastest) way to summarize data.
proc summary data=have nway;
class id ;
output
min(date)=date_first_target
sum(target above_target)=
out=want;
;
run;
data want;
set want;
if above_target or not target then do;
date_first_target=.;
number_target=0;
end;
else number_target=target;
keep id date_first_target number_target ;
run;
Note that if the data is sorted by ID then you can use a BY statement instead of CLASS statement in the PROC SUMMARY step.
Untested, but something like:
data want;
set have;
by id;
retain _date_first_target;
if first.id then call missing(_date_first_target,_number_target,_numberabove);
_number_target++target;
_numberabove++above_target;
if target=1 and missing(_date_first_target) then _date_first_target=date;
if last.id then do;
if _number_above = 0 then do;
date_first_target=_date_first_target;
number_target=_number_target;
end;
else if _number_above>0 then do;
number_target=0;
end;
output;
end;
drop _: ;
run;
try something like this:
data have;
input id date date9. target above_target;
datalines;
1 24MAY2015 0 0
1 25MAY2015 0 1
1 26MAY2015 1 0
2 01JAN2016 1 0
2 02JAN2016 0 0
2 03JAN2016 1 0
3 27MAR2015 0 0
3 28MAR2015 0 0
4 26JUN2016 0 1
4 27JUN2016 0 1
;
run;
proc print;
run;
data want;
merge
have(IN=ABOVE where=(above_target))
have;
by id;
/* for IDs without "above target" do: */
if not ABOVE then
do;
/* get first date */
if target AND missing(date_first_target) then date_first_target = date;
/* count targets */
number_target + target;
end;
if last.id;
output;
/* maintenance */
date_first_target=.;
number_target=0;
retain date_first_target .;
keep id date_first_target number_target;
format date_first_target date9.;
run;
proc print;
run;
Bart
PROC SUMMARY is the simplest (and generally fastest) way to summarize data.
proc summary data=have nway;
class id ;
output
min(date)=date_first_target
sum(target above_target)=
out=want;
;
run;
data want;
set want;
if above_target or not target then do;
date_first_target=.;
number_target=0;
end;
else number_target=target;
keep id date_first_target number_target ;
run;
Note that if the data is sorted by ID then you can use a BY statement instead of CLASS statement in the PROC SUMMARY step.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.
Ready to level-up your skills? Choose your own adventure.