<?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: Aggregation using hashing: counting distinct occurrences based on a string in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Aggregation-using-hashing-counting-distinct-occurrences-based-on/m-p/632745#M187612</link>
    <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/315410"&gt;@left&lt;/a&gt;&amp;nbsp;: Just add _n_alive to the data portion of OUT and and CALL MISSING and also add:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt; _n_alive &lt;SPAN class="token operator"&gt;= &lt;/SPAN&gt;&lt;SPAN class="token function"&gt;sum &lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;_n_alive&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt; Status =: "A"&lt;SPAN class="token punctuation"&gt;) &lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;in the same vein as _n and _sum.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;Paul D.&lt;/P&gt;</description>
    <pubDate>Tue, 17 Mar 2020 17:42:08 GMT</pubDate>
    <dc:creator>hashman</dc:creator>
    <dc:date>2020-03-17T17:42:08Z</dc:date>
    <item>
      <title>Aggregation using hashing: counting distinct occurrences based on a string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Aggregation-using-hashing-counting-distinct-occurrences-based-on/m-p/632697#M187592</link>
      <description>&lt;P&gt;Being new to the concept of "hashing" I want to achieve the following with the dataset [sashelp.heart]:&amp;nbsp;&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;Define the by-group/ key-variables as&amp;nbsp;[smoking_status, bp_status, sex]&lt;/LI&gt;&lt;LI&gt;Aggregate a numeric variable [systolic]&lt;/LI&gt;&lt;LI&gt;Count the number of items in each by-group/ key in new variable [_n]&lt;/LI&gt;&lt;LI&gt;Count the number of distinct values for [systolic] within each by-group/ key in new variable [_unq]&lt;/LI&gt;&lt;LI&gt;Additionally: Get the number of patients alive from dichotomous character variable [Status], i.e. Status="Alive" for each by-group/ key in new variable [_n_alive]&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I already have thanks to&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/21262"&gt;@hashman&lt;/a&gt;&amp;nbsp;and&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13569"&gt;@DonH&lt;/a&gt;&amp;nbsp;(&lt;A title="Data Aggregation using the SAS Hash Object" href="https://support.sas.com/resources/papers/proceedings15/2000-2015.pdf" target="_blank" rel="noopener"&gt;Data Aggregation using the SAS Hash Object&lt;/A&gt;) is points 1 - 4 with the following:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;lt;-- Load the sample data --&amp;gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _heart_src;
    set sashelp.heart (keep=smoking_status bp_status sex systolic status);
    label   sex="Sex"
            status="Survival status"
            systolic="Systolic";
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Dataset at start" style="width: 451px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/36938iF75A53EB39E372C3/image-size/large?v=v2&amp;amp;px=999" role="button" title="hashing_heart_data.png" alt="Dataset at start" /&gt;&lt;span class="lia-inline-image-caption" onclick="event.preventDefault();"&gt;Dataset at start&lt;/span&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;lt;-- Aggregation using hashing --&amp;gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* 01 */ data _null_;
/* 02 */     declare hash out(ordered: "a");
/* 03 */     out.defineKey ("smoking_status", "bp_status", "sex");
/* 04 */     out.defineData("smoking_status", "bp_status", "sex", 
/* 05 */                    "_sum", "_unq", "_n", "_mean");
/* 06 */     out.defineDone();
/* 07 */ 
/* 08 */     declare hash unq();
/* 09 */     unq.defineKey("smoking_status", "bp_status", "sex", "systolic");
/* 10 */     unq.defineDone();
/* 11 */ 
/* 12 */     do until (end);
/* 13 */         set _heart_src end=end;
/* 14 */         if (out.find() ne 0) then call missing(_sum, _unq, _n);
/* 15 */         _sum=sum(_sum, systolic);
/* 16 */         _n=sum(_n, 1);
/* 17 */         if (unq.check() ne 0) then do;
/* 18 */             _unq=sum(_unq, 1);
/* 19 */             unq.add();
/* 20 */         end;
/* 21 */         _mean=_sum/_n;
/* 22 */         out.replace();
/* 23 */     end;
/* 24 */     out.output(DATASET: "_hash_agg");
/* 25 */     format _mean 8.1;
/* 26 */     label  _mean="Mean (systolic)"
/* 27 */            _sum="Sum (systolic)"
/* 28 */            _unq="Unique values (systolic)"
/* 29 */            _n="N (# of subjects)";
/* 30 */ run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;Note: I added line numbers for later references (if needed).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This results in:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="hashing_aggregated_result.png" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/36939iE788C4FA8B616108/image-size/large?v=v2&amp;amp;px=999" role="button" title="hashing_aggregated_result.png" alt="hashing_aggregated_result.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I don't know how to implement point 5 (number of patients alive for each by-group/ key).&lt;/P&gt;&lt;P&gt;I think another hash table and maybe a hash iterator is needed.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I can solve point 5 with a DoW-Loop (no hashing, previous sorting necessary). I guess my code could surely be optimized (please add your comments). Maybe counting variables could be handled with "suminc" and *.sum method?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Another shortcoming in my understanding is: When is the hash table [out] populated as no dataset is directly assigned in the definition? At the moment the first [out.find()] method is called? Is it then, that the whole dataset [_heart_src] is loaded?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 17 Mar 2020 15:19:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Aggregation-using-hashing-counting-distinct-occurrences-based-on/m-p/632697#M187592</guid>
      <dc:creator>left</dc:creator>
      <dc:date>2020-03-17T15:19:33Z</dc:date>
    </item>
    <item>
      <title>Re: Aggregation using hashing: counting distinct occurrences based on a string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Aggregation-using-hashing-counting-distinct-occurrences-based-on/m-p/632713#M187596</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/315410"&gt;@left&lt;/a&gt;&amp;nbsp;Hi and welcome to the SAS Communities &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;First off, I like your approach. This is a nice task to get a grasp of the hash object and it seems you're well on your way.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As to your question on when the hash object out is populated.. This happens in the out.replace() line. Not on the first find() call. As the doc says: The Replace method &lt;EM&gt;"Replaces the data that is associated with the specified key with new data."&lt;/EM&gt;. Read more in the &lt;A href="https://documentation.sas.com/?docsetId=lecompobjref&amp;amp;docsetTarget=p0zjo5imt7rb9rn1npxm1rsl58b3.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en" target="_self"&gt;Replace Method Documentation&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It seems that you have found a way to solve point 5. Though you would still have to add the new variable to the hash object.&lt;/P&gt;</description>
      <pubDate>Tue, 17 Mar 2020 16:09:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Aggregation-using-hashing-counting-distinct-occurrences-based-on/m-p/632713#M187596</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2020-03-17T16:09:10Z</dc:date>
    </item>
    <item>
      <title>Re: Aggregation using hashing: counting distinct occurrences based on a string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Aggregation-using-hashing-counting-distinct-occurrences-based-on/m-p/632717#M187598</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31304"&gt;@PeterClemmensen&lt;/a&gt;Thank you for your quick reply and further explanations!&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It seems to me as a mighty tool. I guess it needs time to get acquainted with all the uses and how things work in concert.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So just to have the final solution fixed (added new variable [_sum_alv] to hash object in line 05 and added new code as line 20a):&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* 01 */ data _null_;
/* 02 */     declare hash out(ordered: "a");
/* 03 */     out.defineKey ("smoking_status", "bp_status", "sex");
/* 04 */     out.defineData("smoking_status", "bp_status", "sex", 
/* 05 */                    "_sum", "_unq", "_sum_alv", "_n", "_mean");
/* 06 */     out.defineDone();
/* 07 */ 
/* 08 */     declare hash unq();
/* 09 */     unq.defineKey("smoking_status", "bp_status", "sex", "systolic");
/* 10 */     unq.defineDone();
/* 11 */ 
/* 12 */     do until (end);
/* 13 */         set _heart_src end=end;
/* 14 */         if (out.find() ne 0) then call missing(_sum, _unq, _n);
/* 15 */         _sum=sum(_sum, systolic);
/* 16 */         _n=sum(_n, 1);
/* 17 */         if (unq.check() ne 0) then do;
/* 18 */             _unq=sum(_unq, 1);
/* 19 */             unq.add();
/* 20 */         end;

/* 20a */        if lowcase(status)="alive" then _sum_alv=sum(_sum_alv, 1);

/* 21 */         _mean=_sum/_n;
/* 22 */         out.replace();
/* 23 */     end;
/* 24 */     out.output(DATASET: "_hash_agg");
/* 25 */     format _mean 8.1;
/* 26 */     label  _mean="Mean (systolic)"
/* 27 */            _sum="Sum (systolic)"
/* 28 */            _unq="Unique values (systolic)"
/* 29 */            _n="N (# of subjects)";
/* 30 */ run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 17 Mar 2020 16:25:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Aggregation-using-hashing-counting-distinct-occurrences-based-on/m-p/632717#M187598</guid>
      <dc:creator>left</dc:creator>
      <dc:date>2020-03-17T16:25:36Z</dc:date>
    </item>
    <item>
      <title>Re: Aggregation using hashing: counting distinct occurrences based on a string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Aggregation-using-hashing-counting-distinct-occurrences-based-on/m-p/632722#M187601</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/315410"&gt;@left&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;First of all, thank you for creating your post in exemplary style.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To implement point 5 you can simply&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;insert &lt;FONT face="courier new,courier"&gt;"_n_alive"&lt;/FONT&gt; into the list of data items in line 5,&lt;/LI&gt;
&lt;LI&gt;insert &lt;FONT face="courier new,courier"&gt;_n_alive&lt;/FONT&gt; into the argument of CALL MISSING in line 14,&lt;/LI&gt;
&lt;LI&gt;insert a sum statement like&lt;BR /&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;_n_alive+(status='Alive');&lt;/CODE&gt;&lt;/PRE&gt;
between lines 16 and 17,&lt;/LI&gt;
&lt;LI&gt;add a label for &lt;FONT face="courier new,courier"&gt;_n_alive&lt;/FONT&gt; in the label statement after line 29 (and move the semicolon accordingly).&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your code is good. To ensure that the DECLARE statements are executed only once, I would insert a STOP statement at the end of the DATA step (before the RUN statement). As a minor simplification you can replace the assignments of the form "&lt;FONT face="courier new,courier"&gt;&lt;EM&gt;var&lt;/EM&gt;=sum(&lt;EM&gt;var&lt;/EM&gt;, &lt;EM&gt;increment&lt;/EM&gt;);&lt;/FONT&gt;" by sum statements of the form "&lt;FONT face="courier new,courier"&gt;&lt;EM&gt;var&lt;/EM&gt;+&lt;EM&gt;increment&lt;/EM&gt;;&lt;/FONT&gt;" (as suggested for &lt;FONT face="courier new,courier"&gt;_n_alive&lt;/FONT&gt; above).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Using &lt;FONT face="courier new,courier"&gt;suminc&lt;/FONT&gt; would be possible, but I don't see a benefit in this case.&lt;/P&gt;</description>
      <pubDate>Tue, 17 Mar 2020 16:37:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Aggregation-using-hashing-counting-distinct-occurrences-based-on/m-p/632722#M187601</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2020-03-17T16:37:48Z</dc:date>
    </item>
    <item>
      <title>Re: Aggregation using hashing: counting distinct occurrences based on a string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Aggregation-using-hashing-counting-distinct-occurrences-based-on/m-p/632728#M187602</link>
      <description>&lt;P&gt;As an additional comment, the great&amp;nbsp;&lt;A href="https://www.sas.com/store/da_dk/books/categories/examples/data-management-solutions-using-sas-hash-table-operations-a-business-intelligence-case-study/prodBK_69153_en.html" target="_self"&gt;Data Management Solutions Using SAS Hash Table Operations: A Business Intelligence Case Study&lt;/A&gt;&amp;nbsp;by the same two authors you mention has an entire chapter on aggretates.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 17 Mar 2020 16:49:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Aggregation-using-hashing-counting-distinct-occurrences-based-on/m-p/632728#M187602</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2020-03-17T16:49:35Z</dc:date>
    </item>
    <item>
      <title>Re: Aggregation using hashing: counting distinct occurrences based on a string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Aggregation-using-hashing-counting-distinct-occurrences-based-on/m-p/632745#M187612</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/315410"&gt;@left&lt;/a&gt;&amp;nbsp;: Just add _n_alive to the data portion of OUT and and CALL MISSING and also add:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt; _n_alive &lt;SPAN class="token operator"&gt;= &lt;/SPAN&gt;&lt;SPAN class="token function"&gt;sum &lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;_n_alive&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt; Status =: "A"&lt;SPAN class="token punctuation"&gt;) &lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;in the same vein as _n and _sum.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;Paul D.&lt;/P&gt;</description>
      <pubDate>Tue, 17 Mar 2020 17:42:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Aggregation-using-hashing-counting-distinct-occurrences-based-on/m-p/632745#M187612</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2020-03-17T17:42:08Z</dc:date>
    </item>
    <item>
      <title>Re: Aggregation using hashing: counting distinct occurrences based on a string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Aggregation-using-hashing-counting-distinct-occurrences-based-on/m-p/632908#M187697</link>
      <description>&lt;P&gt;Thank you for all of your feedback,&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31304"&gt;@PeterClemmensen&lt;/a&gt;&amp;nbsp;,&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt;&amp;nbsp;and&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/21262"&gt;@hashman&lt;/a&gt;!&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've updated the code, but would like to give one more enhancement to it: Instead of having only the summarized data (n=36 by-groups) I want to create the dataset with the full detail information along with the summarized data without creating another datastep.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can it be done without passing through the data once again by creating further hash tables?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I think how it should work (but cannot code it):&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;Create another hash object [detail] and add entries from first DoW-Loop&lt;/LI&gt;&lt;LI&gt;Aggregated data are populated in hash table [out] (already implemented).&lt;/LI&gt;&lt;LI&gt;Update hash table [detail] using [multidata: "yes"] with matching keys from [out] with [detail.ref()].&lt;BR /&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;    dcl hash detail (ordered: "a", multidata: "y");
    detail.defineKey("smoking_status", "bp_status", "sex");
    detail.defineData("smoking_status", "bp_status", "sex", "systolic", "status", 
                      "_sum", "_mean", "_usys", "_n", "_sum_alv");
    detail.defineDone();&lt;/CODE&gt;&lt;/PRE&gt;&lt;/LI&gt;&lt;LI&gt;Output of hash table [detail]&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So using pictures - this is what I have:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Have" style="width: 631px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/36968i0DA158F6703803AE/image-size/large?v=v2&amp;amp;px=999" role="button" title="hashing_aggregated_result_revisited.png" alt="Have" /&gt;&lt;span class="lia-inline-image-caption" onclick="event.preventDefault();"&gt;Have&lt;/span&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is what I want:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Want" style="width: 763px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/36967iF904C607A4C524F5/image-size/large?v=v2&amp;amp;px=999" role="button" title="hashing_heart_data_detailed.png" alt="Want" /&gt;&lt;span class="lia-inline-image-caption" onclick="event.preventDefault();"&gt;Want&lt;/span&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Updated code (what I have so far)&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* 01 */ data _null_;
/* 02 */     declare hash out(ordered: "a");
/* 03 */     out.defineKey ("smoking_status", "bp_status", "sex");
/* 04 */     out.defineData("smoking_status", "bp_status", "sex", 
/* 05 */                    "_sum", "_unq", "_sum_alv", "_n", "_mean");
/* 06 */     out.defineDone();
/* 07 */ 
/* 08 */     declare hash unq();
/* 09 */     unq.defineKey("smoking_status", "bp_status", "sex", "systolic");
/* 10 */     unq.defineDone();
/* 11 */ 
/* 12 */     do until (end);
/* 13 */         set _heart_src end=end;
/* 14 */         if (out.find() ne 0) then call missing(_sum, _unq, _n);
/* 15 */         _sum+systolic;             * _sum=sum(_sum, systolic);
/* 16 */         _n+1;                      * _n=sum(_n, 1);
/* 17 */         if (unq.check() ne 0) then do;
/* 18 */             _unq+1;                * _unq=sum(_unq, 1);
/* 19 */             unq.add();
/* 20 */         end;

/* 20a */        _sum_alv=sum(_sum_alv, status =: "A");     * _sum_alv+(status =: "A");

/* 21 */         _mean=_sum/_n;
/* 22 */         out.replace();
/* 23 */     end;
/* 24 */     out.output(DATASET: "_hash_agg");
/* 25 */     format _mean 8.1;
/* 26 */     label  _mean="Mean (systolic)"
/* 27 */            _sum="Sum (systolic)"
/* 28 */            _unq="Unique values (systolic)"
/* 29 */            _n="N (# of subjects)"

/* 29a */           _sum_alv="Patients alive";

/* 30 */ run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;So my questions:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;How can this be done with hashing?&lt;/LI&gt;&lt;LI&gt;If yes, is a further DoW-Loop required or can it be done in a single run?&amp;nbsp;&lt;/LI&gt;&lt;/UL&gt;</description>
      <pubDate>Wed, 18 Mar 2020 10:39:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Aggregation-using-hashing-counting-distinct-occurrences-based-on/m-p/632908#M187697</guid>
      <dc:creator>left</dc:creator>
      <dc:date>2020-03-18T10:39:09Z</dc:date>
    </item>
  </channel>
</rss>

