<?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: Using first. and last. statements to count a sub-variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Using-first-and-last-statements-to-count-a-sub-variable/m-p/957726#M373847</link>
    <description>&lt;P&gt;Yes, you can use the code above I sent, or one of many other ways, e.g.,&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
select patient, sum(medication='Aspirin') as count
from have
group by patient;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 30 Jan 2025 21:11:06 GMT</pubDate>
    <dc:creator>quickbluefish</dc:creator>
    <dc:date>2025-01-30T21:11:06Z</dc:date>
    <item>
      <title>Using first. and last. statements to count a sub-variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-first-and-last-statements-to-count-a-sub-variable/m-p/957723#M373844</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;I'm trying to count total aspirin dosages per patient in a dataset for which each patient has one entry per day.&amp;nbsp; Code is below. As you can probably gather, I'm getting counts of 1 in each "count' cell rather than an incremental total for each patient. Any help you might provide would be appreciated. Thanks.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;DATA Count;&lt;BR /&gt;SET Medications;&lt;BR /&gt;BY Patient;&lt;BR /&gt;IF First.Patient THEN DO;&lt;BR /&gt;IF Medication = 'Aspirin' THEN DO;&lt;BR /&gt;Count = 0;&lt;BR /&gt;Count + 1; IF LAST.SubjectNumber; END; END;&lt;BR /&gt;RUN;&lt;/P&gt;</description>
      <pubDate>Thu, 30 Jan 2025 20:50:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-first-and-last-statements-to-count-a-sub-variable/m-p/957723#M373844</guid>
      <dc:creator>Newtrix</dc:creator>
      <dc:date>2025-01-30T20:50:35Z</dc:date>
    </item>
    <item>
      <title>Re: Using first. and last. statements to count a sub-variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-first-and-last-statements-to-count-a-sub-variable/m-p/957724#M373845</link>
      <description>&lt;P&gt;This is one of those things where proper indentation really helps.&amp;nbsp; Though if you're just trying to get total number of aspirin administrations by person, you can just do this.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
DATA Count;
SET Medications;
BY Patient;
IF First.Patient THEN count=0;
count+(medication='Aspirin');
if last.patient then output;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;...that said, it's not clear to me from your code / description what the difference is between 'Patient' and 'SubjectNumber'&lt;/P&gt;</description>
      <pubDate>Thu, 30 Jan 2025 20:59:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-first-and-last-statements-to-count-a-sub-variable/m-p/957724#M373845</guid>
      <dc:creator>quickbluefish</dc:creator>
      <dc:date>2025-01-30T20:59:11Z</dc:date>
    </item>
    <item>
      <title>Re: Using first. and last. statements to count a sub-variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-first-and-last-statements-to-count-a-sub-variable/m-p/957725#M373846</link>
      <description>&lt;P&gt;Yeah, I was trying to clarify the code and missed a spot. SubjectNumber should be Patient. Each patient has multiple records, one per day. I'm trying to count the days during which each patient was given aspirin. The dataset looks something like what I have below. Thanks.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Patient&amp;nbsp; Date&amp;nbsp; Medication&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Jan 1&amp;nbsp; &amp;nbsp; &amp;nbsp;Aspirin&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Jan 2&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Jan 3&amp;nbsp; &amp;nbsp; Aspirin&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Jan 5&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Jan 6&amp;nbsp; &amp;nbsp; Aspirin&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Jan 7&amp;nbsp;&lt;/P&gt;&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Jan 10&amp;nbsp; Aspirin&lt;/P&gt;&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Jan 11&amp;nbsp; &amp;nbsp; Aspirin&lt;/P&gt;&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Jan 12&lt;/P&gt;</description>
      <pubDate>Thu, 30 Jan 2025 21:05:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-first-and-last-statements-to-count-a-sub-variable/m-p/957725#M373846</guid>
      <dc:creator>Newtrix</dc:creator>
      <dc:date>2025-01-30T21:05:36Z</dc:date>
    </item>
    <item>
      <title>Re: Using first. and last. statements to count a sub-variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-first-and-last-statements-to-count-a-sub-variable/m-p/957726#M373847</link>
      <description>&lt;P&gt;Yes, you can use the code above I sent, or one of many other ways, e.g.,&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
select patient, sum(medication='Aspirin') as count
from have
group by patient;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 30 Jan 2025 21:11:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-first-and-last-statements-to-count-a-sub-variable/m-p/957726#M373847</guid>
      <dc:creator>quickbluefish</dc:creator>
      <dc:date>2025-01-30T21:11:06Z</dc:date>
    </item>
    <item>
      <title>Re: Using first. and last. statements to count a sub-variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-first-and-last-statements-to-count-a-sub-variable/m-p/957727#M373848</link>
      <description>&lt;P&gt;If your observations are "once per day" and you want to count days then count of observations should work. Which quite means that Proc Freq is likely to be viable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You haven't stated if you actually need a data set or not as output though.&lt;/P&gt;
&lt;PRE&gt;Proc freq data=medications;
   tables Patient*medication /list nopercent nocum;
run;&lt;/PRE&gt;
&lt;P&gt;If you might have other values in the medication variable then restrict to counting Aspirin values only with a where clause&lt;/P&gt;
&lt;PRE&gt;Proc freq data=medications;
   where medication='Aspirin';
   tables Patient*medication /list nopercent nocum;
run;&lt;/PRE&gt;
&lt;P&gt;OUT option on the Tables statement would create a data set&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As an aside: "dates" without years are a poor choice.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 30 Jan 2025 21:29:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-first-and-last-statements-to-count-a-sub-variable/m-p/957727#M373848</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2025-01-30T21:29:21Z</dc:date>
    </item>
    <item>
      <title>Re: Using first. and last. statements to count a sub-variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-first-and-last-statements-to-count-a-sub-variable/m-p/957728#M373849</link>
      <description>&lt;P&gt;If you have more than one Aspirin record per day you might need to include DATE in your BY statement.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  by patient date;
  retain count any;
  if first.patient then count=0;
  if first.date then any=0;
  if medication='Aspirin' then any=1;
  if last.date then count+any;
  if last.patient;
  keep patient count;
run;
        &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 30 Jan 2025 21:31:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-first-and-last-statements-to-count-a-sub-variable/m-p/957728#M373849</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-01-30T21:31:37Z</dc:date>
    </item>
    <item>
      <title>Re: Using first. and last. statements to count a sub-variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-first-and-last-statements-to-count-a-sub-variable/m-p/957772#M373860</link>
      <description>&lt;P&gt;Writing ugly spaghetti code like this is a recipe for problems.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA Count;
SET Medications;
BY Patient;
IF First.Patient THEN DO;
  IF Medication = 'Aspirin' THEN DO;
    Count = 0;
    Count + 1; 
    IF LAST.SubjectNumber;
  END;
END;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now you can clearly see that the increment is part of the FIRST. block and therefore executed only once per patient.&lt;/P&gt;
&lt;P&gt;You also used a variable not included in the BY for your LAST.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data count;
set medications;
by patient;
if first.patient then count = 0;
if medication = 'Aspirin' then count + 1;
if last.patient;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 31 Jan 2025 08:07:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-first-and-last-statements-to-count-a-sub-variable/m-p/957772#M373860</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2025-01-31T08:07:08Z</dc:date>
    </item>
  </channel>
</rss>

