<?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: Conditionally count occurrences in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-count-occurrences/m-p/450561#M113442</link>
    <description>&lt;P&gt;PS if you attach a text file, also supply the code that reads it into SAS, so we get an exact replica of your dataset.&lt;/P&gt;</description>
    <pubDate>Tue, 03 Apr 2018 10:25:09 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2018-04-03T10:25:09Z</dc:date>
    <item>
      <title>Conditionally count occurrences</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-count-occurrences/m-p/450498#M113422</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;&lt;P&gt;I am working on the prescription dataset from a health organization.&amp;nbsp; the dataset contains the following variables in the snapshot&amp;nbsp;below.&amp;nbsp; I want to count a number prescription per patient based on the following criteria,&amp;nbsp;&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;If the deliv_date2 is less than seven and the same ATC code for the individual patient then set a number of prescription is equal to one and sum DDD otherwise count the&amp;nbsp;number of prescription.&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;Thank you in advance!!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data thesis.presc2;
set thesis.presc;
by patient;
if first.patient then lastdate=.;
else lastdate = lag(deliv_date2);
datediff = deliv_date2-lastdate;
run;

proc sql;
CREATE table thesis.presc3 as
select patient,&amp;nbsp; reimburs_cat, source,&amp;nbsp; atc, AdminRoute, N_presc, count(N_presc) as Co_Pres, Center, deliv_date2, DDD,datediff 
From thesis.presc2
where datediff &amp;gt;= 7 
group by patient ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture.PNG" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/19572i8C53AE30B601D87A/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture.PNG" alt="Capture.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 03 Apr 2018 07:34:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-count-occurrences/m-p/450498#M113422</guid>
      <dc:creator>Yilikal_Tesfaye</dc:creator>
      <dc:date>2018-04-03T07:34:14Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally count occurrences</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-count-occurrences/m-p/450508#M113426</link>
      <description>&lt;P&gt;Note that I've edited your post to make your question more description. "help" is fairly generic and makes it hard to search for similar answers in the future.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please post your data as text, especially if you would like tested code as an answer. Currently, to work with your code or come up with a solution I'd have to type it out or mock up my own. Instructions on how include your data as a data step are here:&lt;/P&gt;
&lt;P&gt;&lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712" target="_blank"&gt;https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It also helps if you post what you would expect as output from your input data so that we can test solutions.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Good Luck.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/202500"&gt;@Yilikal_Tesfaye&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am working on the prescription dataset from a health organization.&amp;nbsp; the dataset contains the following variables in the snapshot&amp;nbsp;below.&amp;nbsp; I want to count a number prescription per patient based on the following criteria,&amp;nbsp;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;If the deliv_date2 is less than seven and the same ATC code for the individual patient then set a number of prescription is equal to one and sum DDD otherwise count the&amp;nbsp;number of prescription.&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;Thank you in advance!!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data thesis.presc2;
set thesis.presc;
by patient;
if first.patient then lastdate=.;
else lastdate = lag(deliv_date2);
datediff = deliv_date2-lastdate;
run;

proc sql;
CREATE table thesis.presc3 as
select patient,&amp;nbsp; reimburs_cat, source,&amp;nbsp; atc, AdminRoute, N_presc, count(N_presc) as Co_Pres, Center, deliv_date2, DDD,datediff 
From thesis.presc2
where datediff &amp;gt;= 7 
group by patient ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture.PNG" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/19572i8C53AE30B601D87A/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture.PNG" alt="Capture.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 03 Apr 2018 02:21:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-count-occurrences/m-p/450508#M113426</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-04-03T02:21:06Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally count occurrences</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-count-occurrences/m-p/450560#M113441</link>
      <description>&lt;P&gt;Your use of the lag() function is incorrect:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if first.patient then lastdate=.;
else lastdate = lag(deliv_date2);
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This will cause a wrong entry for lastdate in the second observation for a patient, as you will get the last deliv_date2 of the previous patient.&lt;/P&gt;
&lt;P&gt;Never use lag() conditionally (unless you have a very specific reason for that skip).&lt;/P&gt;
&lt;P&gt;Do it like that:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;lastdate = lag(deliv_date2);
if first.patient then lastdate = .;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 03 Apr 2018 10:22:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-count-occurrences/m-p/450560#M113441</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-04-03T10:22:24Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally count occurrences</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-count-occurrences/m-p/450561#M113442</link>
      <description>&lt;P&gt;PS if you attach a text file, also supply the code that reads it into SAS, so we get an exact replica of your dataset.&lt;/P&gt;</description>
      <pubDate>Tue, 03 Apr 2018 10:25:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-count-occurrences/m-p/450561#M113442</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-04-03T10:25:09Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally count occurrences</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-count-occurrences/m-p/450707#M113517</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/202500"&gt;@Yilikal_Tesfaye&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am working on the prescription dataset from a health organization.&amp;nbsp; the dataset contains the following variables in the snapshot&amp;nbsp;below.&amp;nbsp; I want to count a number prescription per patient based on the following criteria,&amp;nbsp;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;If the &lt;STRONG&gt;&lt;FONT color="#ff0000"&gt;deliv_date2 is less than seven&lt;/FONT&gt; &lt;/STRONG&gt;and the same ATC code for the individual patient then set a number of prescription is equal to one and sum DDD otherwise count the&amp;nbsp;number of prescription.&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;Thank you in advance!!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Incomplete specification for the highlighted bit. I think many of us will assume less than 7 days given that it is a date but what specific other date(s) is it to be compared with?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You may be interested to know there is a function DIF that works like LAG to do the difference of the current value from the previous. So you can use:&lt;/P&gt;
&lt;P&gt;datediff &lt;SPAN class="token operator"&gt;= dif(&lt;/SPAN&gt;&amp;nbsp;deliv_date2);&lt;/P&gt;
&lt;P&gt;Same note about conditional use from &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt; about LAG applies.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 03 Apr 2018 15:35:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-count-occurrences/m-p/450707#M113517</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-04-03T15:35:01Z</dc:date>
    </item>
    <item>
      <title>Conditionally count occurrences</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-count-occurrences/m-p/450924#M113610</link>
      <description>&lt;P&gt;Dear Sir/ Madam,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;DIV&gt;&amp;nbsp;I want to compute the number prescription per patient.&amp;nbsp; I would like to use the following criteria count the prescription:&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;1. If the&amp;nbsp;deliv_date2 was within seven days per patient(i.e less than 7 days)&amp;nbsp; and the&amp;nbsp; ATC code similar then set the number of&amp;nbsp;prescription equal to one and sum DDD.&amp;nbsp; otherwise, count the DDD per patient and set the count to the number of prescription.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; I tried using the following&amp;nbsp;code but I didn't get what I want.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data thesis.presc2;
set thesis.presc;
by patient;
lastdate = lag(deliv_date2);
LAG_DDD = LAG(DDD);
if first.patient then lastdate = .;
datediff = deliv_date2-lastdate;
run;

data thesis.presc21;
set thesis.presc2;
by patient ;
If atc = lag(atc) and datediff &amp;lt; 7 then
Sum_DDD = sum(LAG_DDD,DDD); 
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture.PNG" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/19603i1167BBCC2E9D6741/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture.PNG" alt="Capture.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;/DIV&gt;</description>
      <pubDate>Wed, 04 Apr 2018 00:58:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-count-occurrences/m-p/450924#M113610</guid>
      <dc:creator>Yilikal_Tesfaye</dc:creator>
      <dc:date>2018-04-04T00:58:25Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally count occurrences</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-count-occurrences/m-p/450945#M113611</link>
      <description>&lt;P&gt;Try something like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data temp;
set have; by patient atc notsorted;
lagDate = lag(date);
if first.atc then seq + 1;
else if intck("day", lagDate, date) &amp;gt; 7 then seq +1;
drop lagdate;
run;

data want;
do until(last.seq);
    set temp; by patient atc seq notsorted;
    newDDD = sum(newDDD, DDD);
    end;
drop seq DDD;
rename newDDD=DDD;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 04 Apr 2018 05:18:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-count-occurrences/m-p/450945#M113611</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2018-04-04T05:18:26Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally count occurrences</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-count-occurrences/m-p/450948#M113612</link>
      <description>&lt;P&gt;I merged the last two posts into the older thread concerning the same issue, as there were already several answers given.&lt;/P&gt;
&lt;P&gt;To repeat myself (what I usually hat to do): if you supply data in text form, also include the code that reads that text into a SAS dataset.&lt;/P&gt;</description>
      <pubDate>Wed, 04 Apr 2018 06:09:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-count-occurrences/m-p/450948#M113612</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-04-04T06:09:19Z</dc:date>
    </item>
  </channel>
</rss>

