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

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 🙂

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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.

View solution in original post

3 REPLIES 3
Quentin
Super User

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;
The Boston Area SAS Users Group is hosting free webinars!
Next webinar will be in January 2025. Until then, check out our archives: https://www.basug.org/videos. And be sure to subscribe to our our email list.
yabwon
Onyx | Level 15

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

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



Tom
Super User Tom
Super User

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.

sas-innovate-wordmark-2025-midnight.png

Register Today!

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.


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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 732 views
  • 0 likes
  • 4 in conversation