<?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: How to calculate number of subjects by different variablesin same data step in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-number-of-subjects-by-different-variablesin/m-p/326521#M72726</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;One way is do this using&amp;nbsp;a single proc sql instead of multiple ones. Please try below:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sql;&lt;BR /&gt;create table want as&lt;BR /&gt;select *,count(distinct id) as NS2&lt;BR /&gt;from (select id,trt1,term,period,count(distinct id) as NS1&lt;BR /&gt;from data1&lt;BR /&gt;group by trt1)&lt;BR /&gt;group by period,trt1,term&lt;BR /&gt;order by id;&lt;BR /&gt;quit;&lt;/P&gt;</description>
    <pubDate>Sat, 21 Jan 2017 21:10:19 GMT</pubDate>
    <dc:creator>stat_sas</dc:creator>
    <dc:date>2017-01-21T21:10:19Z</dc:date>
    <item>
      <title>How to calculate number of subjects by different variablesin same data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-number-of-subjects-by-different-variablesin/m-p/326518#M72723</link>
      <description>&lt;P&gt;Dear,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I neeed to calculate number of subjects by different sort order &amp;nbsp;to calculate percentages.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am running following codes to calculate the NS and then merge with main data set by the sort order. It will be a 4 steps and I have to calcualte many. So I want tto know if I can do in one step. Please suggest help. Thanks.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sql;&lt;BR /&gt;create table NS1 as&lt;BR /&gt;select count(distinct id) as NS1,trt1&lt;BR /&gt;from data1&lt;BR /&gt;group by trt1;&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;proc sql;&lt;BR /&gt;create table NS2 as&lt;BR /&gt;select count(distinct id) as NS2,term,trt1,period&lt;BR /&gt;from adae3&lt;BR /&gt;group by period,trt1,term;&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data1;&lt;/P&gt;&lt;P&gt;id &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;term &amp;nbsp; &amp;nbsp; &amp;nbsp; trt1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;period&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;ge &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 100 mg &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;/P&gt;&lt;P&gt;2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ge &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;200 mg &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;/P&gt;&lt;P&gt;3 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ge &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 100 mg &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;/P&gt;&lt;P&gt;4 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ge &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;200 mg &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;/P&gt;&lt;P&gt;5 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ae &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;100 mg &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;/P&gt;&lt;P&gt;6 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ae &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 300 mg &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;output needed:&lt;/P&gt;&lt;P&gt;data1;&lt;/P&gt;&lt;P&gt;id &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;term &amp;nbsp; &amp;nbsp; &amp;nbsp; trt1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;period &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ns1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ns2&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;ge &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 100 mg &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&lt;/P&gt;&lt;P&gt;2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ge &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;200 mg &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&lt;/P&gt;&lt;P&gt;3 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ge &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 100 mg &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 3 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2&lt;/P&gt;&lt;P&gt;4 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ge &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;200 mg &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2&lt;/P&gt;&lt;P&gt;5 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ae &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;100 mg &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;/P&gt;&lt;P&gt;6 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ae &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 300 mg &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 21 Jan 2017 20:16:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-number-of-subjects-by-different-variablesin/m-p/326518#M72723</guid>
      <dc:creator>knveraraju91</dc:creator>
      <dc:date>2017-01-21T20:16:05Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate number of subjects by different variablesin same data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-number-of-subjects-by-different-variablesin/m-p/326521#M72726</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;One way is do this using&amp;nbsp;a single proc sql instead of multiple ones. Please try below:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sql;&lt;BR /&gt;create table want as&lt;BR /&gt;select *,count(distinct id) as NS2&lt;BR /&gt;from (select id,trt1,term,period,count(distinct id) as NS1&lt;BR /&gt;from data1&lt;BR /&gt;group by trt1)&lt;BR /&gt;group by period,trt1,term&lt;BR /&gt;order by id;&lt;BR /&gt;quit;&lt;/P&gt;</description>
      <pubDate>Sat, 21 Jan 2017 21:10:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-number-of-subjects-by-different-variablesin/m-p/326521#M72726</guid>
      <dc:creator>stat_sas</dc:creator>
      <dc:date>2017-01-21T21:10:19Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate number of subjects by different variablesin same data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-number-of-subjects-by-different-variablesin/m-p/326547#M72734</link>
      <description>&lt;P&gt;Below code sample uses a hash objects for the counts to allow for implementation in a single data step.&lt;/P&gt;
&lt;P&gt;Make sure you define in the Length statement for _vars and _keys a length which is sufficient to hold the max. strings for variable name combinations and values (100 chars used in below sample).&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  infile datalines truncover;
  input (id term trt1 period) ($);
  datalines;
1 ge 100 mg 1
2 ge 200 mg 1
3 ge 100 mg 1
4 ge 200 mg 1
5 ae 100 mg 1
6 ae 300 mg 1
;
run;

data want(drop=_:);

  if _n_=1 then
    do;
      if 0 then set have;
      length _vars $100 _key $100 _count 8;
      dcl hash h1(multidata:'r',suminc:'_count');
      h1.defineKey('_vars','_key');
      h1.defineDone();

      _count=1;
      do while(last ne 1);
        set have end=last;
        /* count by trt1 */
        _vars=catx('|','trt1');
        _key=catx('|',trt1);
        _rc=h1.ref();

        /*count by period,trt1,term */
        _vars=catx('|','period','trt1','term');
        _key=catx('|',period,trt1,term);
        _rc=h1.ref();
      end;
      call missing(of _all_);
    end;

    set have;

    /* get count by trt1 */
    _vars=catx('|','trt1');
    _key=catx('|',trt1);
    _rc=h1.sum(sum: ns1);

    /* get count by  period,trt1,term */
    _vars=catx('|','period','trt1','term');
    _key=catx('|',period,trt1,term);
    _rc=h1.sum(sum: ns2);

  run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;N.B: Unlike in the SQL you've posted, above code counts ALL occurences and not only per DISTINCT ID. Given that ID's should be unique (and that they are in your sample data) things should still work as expected.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 22 Jan 2017 03:10:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-number-of-subjects-by-different-variablesin/m-p/326547#M72734</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2017-01-22T03:10:02Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate number of subjects by different variablesin same data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-number-of-subjects-by-different-variablesin/m-p/326900#M72886</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;For simplicity I dropped period, not had to add.
Can easily add other cross tabs.

proc summary data=sd1.have n;
class trt1 term;
types trt1 trt1*term;
output out=want_pre(rename=_freq_=count drop=id _type_);
id id;
run;quit;

proc sql;
  create
      table wrk.want as
  select
      l.id
     ,l.trt1
     ,l.term
     ,c.count as ns1
     ,r.count as ns2
  from
     sd1.have as l
       left join want_pre as c on l.trt1=c.trt1 and c.term=""
       left join want_pre as r on l.trt1=r.trt1 and l.term=r.term and l.term ne ""
  order
     by id
;quit;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 24 Jan 2017 01:02:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-number-of-subjects-by-different-variablesin/m-p/326900#M72886</guid>
      <dc:creator>rogerjdeangelis</dc:creator>
      <dc:date>2017-01-24T01:02:49Z</dc:date>
    </item>
  </channel>
</rss>

