<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: count number of times within a certain value in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/count-number-of-times-within-a-certain-value/m-p/874281#M345410</link>
    <description>&lt;P&gt;try something like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
    <pubDate>Sat, 06 May 2023 20:54:35 GMT</pubDate>
    <dc:creator>yabwon</dc:creator>
    <dc:date>2023-05-06T20:54:35Z</dc:date>
    <item>
      <title>count number of times within a certain value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/count-number-of-times-within-a-certain-value/m-p/874261#M345396</link>
      <description>&lt;P&gt;Hi all.&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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.&amp;nbsp;&lt;/P&gt;&lt;P&gt;The date is in the format date9.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data have;&lt;/P&gt;&lt;P&gt;input id date target above_target;&lt;/P&gt;&lt;P&gt;datalines;&lt;/P&gt;&lt;P&gt;1 24MAY2015 0 0&lt;/P&gt;&lt;P&gt;1 25MAY2015 0 1&lt;/P&gt;&lt;P&gt;1 26MAY2015 1 0&lt;/P&gt;&lt;P&gt;2 01JAN2016 1 0&lt;/P&gt;&lt;P&gt;2 02JAN2016 0 0&lt;/P&gt;&lt;P&gt;2 03JAN2016 1 0&lt;/P&gt;&lt;P&gt;3 27MAR2015 0 0&lt;/P&gt;&lt;P&gt;3 28MAR2015 0 0&lt;/P&gt;&lt;P&gt;4 26JUN2016 0 1&amp;nbsp;&lt;/P&gt;&lt;P&gt;4 27JUN206 0 1&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data want;&lt;/P&gt;&lt;P&gt;input id date_first_target number_target;&lt;/P&gt;&lt;P&gt;datalines;&lt;/P&gt;&lt;P&gt;1 . . 0&lt;/P&gt;&lt;P&gt;2 01JAN2016 2&lt;/P&gt;&lt;P&gt;3 . . 0&lt;/P&gt;&lt;P&gt;4 . . 0&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I hope you can help me, thanks &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 06 May 2023 12:20:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/count-number-of-times-within-a-certain-value/m-p/874261#M345396</guid>
      <dc:creator>lone0708</dc:creator>
      <dc:date>2023-05-06T12:20:47Z</dc:date>
    </item>
    <item>
      <title>Re: count number of times within a certain value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/count-number-of-times-within-a-certain-value/m-p/874279#M345409</link>
      <description>&lt;P&gt;Untested, but something like:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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&amp;gt;0 then do;
      number_target=0;
    end;
    output;
  end;

  drop _: ;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 06 May 2023 18:19:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/count-number-of-times-within-a-certain-value/m-p/874279#M345409</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2023-05-06T18:19:46Z</dc:date>
    </item>
    <item>
      <title>Re: count number of times within a certain value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/count-number-of-times-within-a-certain-value/m-p/874281#M345410</link>
      <description>&lt;P&gt;try something like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Sat, 06 May 2023 20:54:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/count-number-of-times-within-a-certain-value/m-p/874281#M345410</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2023-05-06T20:54:35Z</dc:date>
    </item>
    <item>
      <title>Re: count number of times within a certain value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/count-number-of-times-within-a-certain-value/m-p/874291#M345413</link>
      <description>&lt;P&gt;PROC SUMMARY is the simplest (and generally fastest) way to summarize data.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;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.&lt;/P&gt;</description>
      <pubDate>Sat, 06 May 2023 19:57:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/count-number-of-times-within-a-certain-value/m-p/874291#M345413</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-05-06T19:57:55Z</dc:date>
    </item>
  </channel>
</rss>

