<?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 Summing over observations...first.id in SAS Health and Life Sciences</title>
    <link>https://communities.sas.com/t5/SAS-Health-and-Life-Sciences/Summing-over-observations-first-id/m-p/2750#M217</link>
    <description>I have data in the following format (two variables: id and dispensed):&lt;BR /&gt;
&lt;BR /&gt;
id dispensed&lt;BR /&gt;
1       yes&lt;BR /&gt;
1       yes&lt;BR /&gt;
1       yes&lt;BR /&gt;
1       yes&lt;BR /&gt;
2       no&lt;BR /&gt;
2       no&lt;BR /&gt;
3       yes&lt;BR /&gt;
....and so on.&lt;BR /&gt;
&lt;BR /&gt;
I need to calculate the total number of 'dispensed' (sum of 'yes' + 'no's). &lt;B&gt;Each id should only be counted once.&lt;/B&gt; So I am looking to calculate something like the following:&lt;BR /&gt;
&lt;BR /&gt;
total_dispensed=3&lt;BR /&gt;
dispensed_yes=2&lt;BR /&gt;
dispensed_no=1&lt;BR /&gt;
&lt;BR /&gt;
How do I calculate this? Thank you very much!</description>
    <pubDate>Mon, 09 Apr 2007 20:07:50 GMT</pubDate>
    <dc:creator>deleted_user</dc:creator>
    <dc:date>2007-04-09T20:07:50Z</dc:date>
    <item>
      <title>Summing over observations...first.id</title>
      <link>https://communities.sas.com/t5/SAS-Health-and-Life-Sciences/Summing-over-observations-first-id/m-p/2750#M217</link>
      <description>I have data in the following format (two variables: id and dispensed):&lt;BR /&gt;
&lt;BR /&gt;
id dispensed&lt;BR /&gt;
1       yes&lt;BR /&gt;
1       yes&lt;BR /&gt;
1       yes&lt;BR /&gt;
1       yes&lt;BR /&gt;
2       no&lt;BR /&gt;
2       no&lt;BR /&gt;
3       yes&lt;BR /&gt;
....and so on.&lt;BR /&gt;
&lt;BR /&gt;
I need to calculate the total number of 'dispensed' (sum of 'yes' + 'no's). &lt;B&gt;Each id should only be counted once.&lt;/B&gt; So I am looking to calculate something like the following:&lt;BR /&gt;
&lt;BR /&gt;
total_dispensed=3&lt;BR /&gt;
dispensed_yes=2&lt;BR /&gt;
dispensed_no=1&lt;BR /&gt;
&lt;BR /&gt;
How do I calculate this? Thank you very much!</description>
      <pubDate>Mon, 09 Apr 2007 20:07:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Health-and-Life-Sciences/Summing-over-observations-first-id/m-p/2750#M217</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2007-04-09T20:07:50Z</dc:date>
    </item>
    <item>
      <title>Re: Summing over observations...first.id</title>
      <link>https://communities.sas.com/t5/SAS-Health-and-Life-Sciences/Summing-over-observations-first-id/m-p/2751#M218</link>
      <description>Hi:&lt;BR /&gt;
  It sort of depends on whether you want a summary dataset as the final result or want the information to be in the detail data. However, assuming that you want just a file of information that can give you the totals and information by ID and then has a summary line, look at the DATA step program and the first PROC REPORT which produces this output:[pre]&lt;BR /&gt;
                       Dispensed Information by ID&lt;BR /&gt;
                                 Dispensed  Dispensed      Total      Total&lt;BR /&gt;
                          Flag       =Yes        =No        Yes         No&lt;BR /&gt;
            Num Obs  Dispensed       This       This       This       This&lt;BR /&gt;
      ID    This ID    This ID         ID         ID         ID         ID&lt;BR /&gt;
       1          4          1          1          0          4          0&lt;BR /&gt;
       2          2          1          0          1          0          2&lt;BR /&gt;
       3          1          1          1          0          1          0&lt;BR /&gt;
          =========  =========  =========  =========  =========  =========&lt;BR /&gt;
                  7          3          2          1          5          2&lt;BR /&gt;
          =========  =========  =========  =========  =========  =========&lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
 To just produce a line of SUMMARY information, look at the DATA step program and the second PROC REPORT which produces this output.[pre]&lt;BR /&gt;
&lt;BR /&gt;
                  Overall Summary Dispensed Information&lt;BR /&gt;
                           Dispensed  Dispensed      Total      Total&lt;BR /&gt;
                    Flag       =Yes        =No        Yes         No&lt;BR /&gt;
      Num Obs  Dispensed        All        All        All        All&lt;BR /&gt;
      All IDs    All IDs        IDs        IDs        IDs        IDs&lt;BR /&gt;
            7          3          2          1          5          2&lt;BR /&gt;
 [/pre]&lt;BR /&gt;
There are other ways to approach this, such as setting flags in the original data, when you read the file, but it sounds like you already have a data file in SAS format. You probably will not want all the calculated variables in the final output, however, I thought it might be useful to show the difference between accumulating a counter for each ID versus setting a numeric flag of 0 or 1 for each ID.&lt;BR /&gt;
  &lt;BR /&gt;
Good luck!&lt;BR /&gt;
cynthia&lt;BR /&gt;
&lt;BR /&gt;
[pre]&lt;BR /&gt;
*** the code;&lt;BR /&gt;
** 1) Make some data;&lt;BR /&gt;
data disp;&lt;BR /&gt;
input id dispensed $;&lt;BR /&gt;
return;&lt;BR /&gt;
datalines;&lt;BR /&gt;
1 yes&lt;BR /&gt;
1 yes&lt;BR /&gt;
1 yes&lt;BR /&gt;
1 yes&lt;BR /&gt;
2 no&lt;BR /&gt;
2 no&lt;BR /&gt;
3 yes&lt;BR /&gt;
;&lt;BR /&gt;
run;&lt;BR /&gt;
                  &lt;BR /&gt;
** 2) Sort by ID;&lt;BR /&gt;
proc sort data=disp;&lt;BR /&gt;
by id;&lt;BR /&gt;
run;&lt;BR /&gt;
                         &lt;BR /&gt;
** 3) Set up some flags to catch the yes, no;&lt;BR /&gt;
**    and at the end of each ID, add them up.;&lt;BR /&gt;
**    Note that logic does not account for scenario;&lt;BR /&gt;
**    where an ID had both yes AND no -- for example,&lt;BR /&gt;
**    if that was an error condition.;&lt;BR /&gt;
**    CALCDISP file is a SUMMARY file.;&lt;BR /&gt;
data calcdisp;&lt;BR /&gt;
  set disp; by id;&lt;BR /&gt;
  retain disp_yes disp_no &lt;BR /&gt;
         num_yes num_no id_cnt;&lt;BR /&gt;
                     &lt;BR /&gt;
  if first.id then do;&lt;BR /&gt;
     disp_yes = 0;&lt;BR /&gt;
     disp_no = 0;&lt;BR /&gt;
     num_yes = 0;&lt;BR /&gt;
     num_no = 0;&lt;BR /&gt;
     id_cnt = 0;&lt;BR /&gt;
  end;&lt;BR /&gt;
  id_cnt + 1;&lt;BR /&gt;
  if dispensed = 'yes' then num_yes + 1;&lt;BR /&gt;
    else if dispensed = 'no' then num_no + 1;&lt;BR /&gt;
  if last.id then do;&lt;BR /&gt;
    if num_yes ge 1 then disp_yes = 1;&lt;BR /&gt;
       else disp_yes = 0;&lt;BR /&gt;
    if num_no ge 1 then disp_no = 1;&lt;BR /&gt;
       else disp_no = 0;&lt;BR /&gt;
    tot_disp = disp_yes + disp_no;&lt;BR /&gt;
    output;&lt;BR /&gt;
  end;&lt;BR /&gt;
run;&lt;BR /&gt;
                        &lt;BR /&gt;
** 4) Now use Proc Report and add up the flags and counters;&lt;BR /&gt;
**    to show the difference between the flags with 1 or 0;&lt;BR /&gt;
**    and the counters for each ID.;&lt;BR /&gt;
proc report data=calcdisp split='*' nowd;&lt;BR /&gt;
  title 'Dispensed Information by ID';&lt;BR /&gt;
  column id id_cnt tot_disp disp_yes &lt;BR /&gt;
         disp_no num_yes num_no;&lt;BR /&gt;
  define id / order 'ID';&lt;BR /&gt;
  define id_cnt / 'Num Obs*This ID';&lt;BR /&gt;
  define tot_disp / 'Flag*Dispensed*This ID';&lt;BR /&gt;
  define disp_yes / 'Dispensed*=Yes*This*ID';&lt;BR /&gt;
  define disp_no / 'Dispensed*=No*This*ID';&lt;BR /&gt;
  define num_yes / 'Total*Yes*This*ID';&lt;BR /&gt;
  define  num_no / 'Total*No*This*ID';&lt;BR /&gt;
  rbreak after /summarize dol dul;&lt;BR /&gt;
run;&lt;BR /&gt;
                            &lt;BR /&gt;
proc report data=calcdisp split='*' nowd;&lt;BR /&gt;
  title 'Overall Summary Dispensed Information';&lt;BR /&gt;
  column id_cnt tot_disp disp_yes &lt;BR /&gt;
         disp_no num_yes num_no;&lt;BR /&gt;
  define id_cnt / 'Num Obs*All IDs';&lt;BR /&gt;
  define tot_disp / 'Flag*Dispensed*All IDs';&lt;BR /&gt;
  define disp_yes / 'Dispensed*=Yes*All*IDs';&lt;BR /&gt;
  define disp_no / 'Dispensed*=No*All*IDs';&lt;BR /&gt;
  define num_yes / 'Total*Yes*All*IDs';&lt;BR /&gt;
  define  num_no / 'Total*No*All*IDs';&lt;BR /&gt;
run;&lt;BR /&gt;
[/pre]</description>
      <pubDate>Mon, 09 Apr 2007 22:22:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Health-and-Life-Sciences/Summing-over-observations-first-id/m-p/2751#M218</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2007-04-09T22:22:40Z</dc:date>
    </item>
  </channel>
</rss>

