<?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: What is an elegant way to count these flags (including zeros for rows that don't have a &amp;quot;Y&amp; in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/What-is-an-elegant-way-to-count-these-flags-including-zeros-for/m-p/871927#M344468</link>
    <description>Does that desired output align with the data? I don't see any TRTAN3 in the data?</description>
    <pubDate>Tue, 25 Apr 2023 16:02:21 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2023-04-25T16:02:21Z</dc:date>
    <item>
      <title>What is an elegant way to count these flags (including zeros for rows that don't have a "Y")?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/What-is-an-elegant-way-to-count-these-flags-including-zeros-for/m-p/871923#M344466</link>
      <description>&lt;P&gt;Hi, the purpose of this exercise is to create a table showing how many subjects populate for each flag. This is a data manipulation exercise, and the main thing i'm struggling with is trying to output a 0 count for flag2. Ideally, I'd like to be able to do it in PROC SQL.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have; 
infile datalines dsd dlm=",";
	input subjid $ trtan flag1 $ flag2 $ flag3 $ flag4 $ flag5 $;
datalines;
001, 1, Y, , , Y, Y
002, 2, , , , ,
003, 1, Y, , Y, Y, 
004, 2, Y, , Y, ,
005, 2, , , , Y,
006, 2, , , , , Y
007, 1, Y, , Y, , 
008, 1, Y, , , , Y
009, 2, Y, , , , 
010, 1, , , y, , 
;
run;


data have1; set have;
output;
trtan=3; output;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;BR /&gt;The output is supposed to have 4 columns, and 5 rows. The first two rows are only supposed to output values for the flag label column and overall column (TRTAN3).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Desired output;&lt;/P&gt;
&lt;P&gt;FLAG &amp;nbsp; TRTAN1 TRTAN2 TRTAN3&lt;/P&gt;
&lt;P&gt;Flag1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 6&lt;/P&gt;
&lt;P&gt;Flag2&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0&lt;/P&gt;
&lt;P&gt;Flag3&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;&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; 4&lt;/P&gt;
&lt;P&gt;Flag4&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;&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; 3&lt;/P&gt;
&lt;P&gt;Flag5&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;&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; 3&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 25 Apr 2023 15:39:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/What-is-an-elegant-way-to-count-these-flags-including-zeros-for/m-p/871923#M344466</guid>
      <dc:creator>Hello_there</dc:creator>
      <dc:date>2023-04-25T15:39:38Z</dc:date>
    </item>
    <item>
      <title>Re: What is an elegant way to count these flags (including zeros for rows that don't have a "Y&amp;</title>
      <link>https://communities.sas.com/t5/SAS-Programming/What-is-an-elegant-way-to-count-these-flags-including-zeros-for/m-p/871926#M344467</link>
      <description>SQL will need to be hardcoded for this type of analysis. Use a different approach here.</description>
      <pubDate>Tue, 25 Apr 2023 15:53:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/What-is-an-elegant-way-to-count-these-flags-including-zeros-for/m-p/871926#M344467</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2023-04-25T15:53:26Z</dc:date>
    </item>
    <item>
      <title>Re: What is an elegant way to count these flags (including zeros for rows that don't have a "Y&amp;</title>
      <link>https://communities.sas.com/t5/SAS-Programming/What-is-an-elegant-way-to-count-these-flags-including-zeros-for/m-p/871927#M344468</link>
      <description>Does that desired output align with the data? I don't see any TRTAN3 in the data?</description>
      <pubDate>Tue, 25 Apr 2023 16:02:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/What-is-an-elegant-way-to-count-these-flags-including-zeros-for/m-p/871927#M344468</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2023-04-25T16:02:21Z</dc:date>
    </item>
    <item>
      <title>Re: What is an elegant way to count these flags (including zeros for rows that don't have a "Y&amp;</title>
      <link>https://communities.sas.com/t5/SAS-Programming/What-is-an-elegant-way-to-count-these-flags-including-zeros-for/m-p/871928#M344469</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have; 
infile datalines dsd dlm=",";
	input subjid $ trtan flag1 $ flag2 $ flag3 $ flag4 $ flag5 $;
datalines;
001, 1, Y, , , Y, Y
002, 2, , , , ,
003, 1, Y, , Y, Y, 
004, 2, Y, , Y, ,
005, 2, , , , Y,
006, 2, , , , , Y
007, 1, Y, , Y, , 
008, 1, Y, , , , Y
009, 2, Y, , , , 
010, 1, , , y, , 
;
run;
proc print;
run;

data have1; set have;
output;
trtan=3; output;
run;
proc print;
run;

data have2;
  set have1;
  N=_N_;
run;

proc transpose data=have2 out=have2t;
  by n trtan;
  var flag:;
run;
proc print;
run;

proc sql;
  create table have3 as
  select trtan, _NAME_ as flag, count(col1) as x
  from have2t
  group by _NAME_, trtan
  ;
quit;

proc transpose data=have3 out=want(drop=_NAME_) prefix=trtan;
  by flag;
  var x;
  id trtan;
run;
proc print;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 25 Apr 2023 16:05:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/What-is-an-elegant-way-to-count-these-flags-including-zeros-for/m-p/871928#M344469</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2023-04-25T16:05:27Z</dc:date>
    </item>
    <item>
      <title>Re: What is an elegant way to count these flags (including zeros for rows that don't have a "Y&amp;</title>
      <link>https://communities.sas.com/t5/SAS-Programming/What-is-an-elegant-way-to-count-these-flags-including-zeros-for/m-p/871935#M344473</link>
      <description>Hi Reeza, it's in the second data step where i output it.</description>
      <pubDate>Tue, 25 Apr 2023 16:18:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/What-is-an-elegant-way-to-count-these-flags-including-zeros-for/m-p/871935#M344473</guid>
      <dc:creator>Hello_there</dc:creator>
      <dc:date>2023-04-25T16:18:59Z</dc:date>
    </item>
    <item>
      <title>Re: What is an elegant way to count these flags (including zeros for rows that don't have a "Y&amp;</title>
      <link>https://communities.sas.com/t5/SAS-Programming/What-is-an-elegant-way-to-count-these-flags-including-zeros-for/m-p/871936#M344474</link>
      <description>Very nice and elegant solution. Thanks, Yabwon!</description>
      <pubDate>Tue, 25 Apr 2023 16:19:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/What-is-an-elegant-way-to-count-these-flags-including-zeros-for/m-p/871936#M344474</guid>
      <dc:creator>Hello_there</dc:creator>
      <dc:date>2023-04-25T16:19:36Z</dc:date>
    </item>
    <item>
      <title>Re: What is an elegant way to count these flags (including zeros for rows that don't have a "Y&amp;</title>
      <link>https://communities.sas.com/t5/SAS-Programming/What-is-an-elegant-way-to-count-these-flags-including-zeros-for/m-p/871938#M344475</link>
      <description>&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="yabwon_0-1682439673542.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/83208i752D3A03C59A6092/image-size/medium?v=v2&amp;amp;px=400" role="button" title="yabwon_0-1682439673542.png" alt="yabwon_0-1682439673542.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 25 Apr 2023 16:21:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/What-is-an-elegant-way-to-count-these-flags-including-zeros-for/m-p/871938#M344475</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2023-04-25T16:21:24Z</dc:date>
    </item>
    <item>
      <title>Re: What is an elegant way to count these flags (including zeros for rows that don't have a "Y&amp;</title>
      <link>https://communities.sas.com/t5/SAS-Programming/What-is-an-elegant-way-to-count-these-flags-including-zeros-for/m-p/871942#M344478</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/252358"&gt;@Hello_there&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi, the purpose of this exercise is to create a table showing how many subjects populate for each flag. This is a data manipulation exercise, &lt;CODE class=" language-sas"&gt;
&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Is this exercise in anyway supposed to be extendable to more observations or variables?&lt;BR /&gt;&lt;BR /&gt;Please note that you have one value of lowercase y in your data. So are we supposed to count just Y or include the y as well?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If this "exercise" is homework what have you actually tried?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What if you create variables that have values of 1 instead of Y and 0 instead of missing (VERY easily done) then sums get counts and 0. But you are reshaping data twice with the totals so do not expect anything "elegant", whatever that means in this&amp;nbsp; case.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I can do such, here is one example but I would not want to extend this for many more "exceptions" to your which flags gets summarized by which Trtan values.&lt;/P&gt;
&lt;PRE&gt;data have; 
infile datalines dsd dlm=",";
	input subjid $ trtan flag1 $ flag2 $ flag3 $ flag4 $ flag5 $;
datalines;
001, 1, Y, , , Y, Y
002, 2, , , , ,
003, 1, Y, , Y, Y, 
004, 2, Y, , Y, ,
005, 2, , , , Y,
006, 2, , , , , Y
007, 1, Y, , Y, , 
008, 1, Y, , , , Y
009, 2, Y, , , , 
010, 1, , , Y, , 
;
run;

data temp;
  set have;
  array f(*) flag: ;
  length row $ 6;
  do i= 1 to dim(f);
     row=vname(f[i]);
     value= (f[i]='Y');
     output;
  end;
  keep row trtan value;
run;

proc summary data=temp;
   class row trtan;
   var value;
   output out=summary sum=;
run;

data want (rename=(row=flag));
   set summary;
   array t (*) trtan1 trtan2;
   retain trtan1 trtan2 trtan3;
   if (row in ('flag1' 'flag2') and _type_=2 )
     or (row in ('flag3' 'flag4' 'flag5') and _type_=3);
   by row notsorted;
   if first.row then call missing(trtan1,trtan2,trtan3);
   if row in ('flag1' 'flag2') then trtan3=value;
   else do;
      t[trtan]=value;
      trtan3=sum(trtan3,value);
   end;
   if last.row;
   keep row trtan1-trtan3;

run;&lt;/PRE&gt;
&lt;P&gt;I wouldn't even contemplate SQL for such personally. One main reason: there is some point where you have to do the same things for multiple variables which typically points to an array and SQL does not support arrays. So leads to extremely verbose code and/or multiple passes through the data.&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, 25 Apr 2023 16:30:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/What-is-an-elegant-way-to-count-these-flags-including-zeros-for/m-p/871942#M344478</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-04-25T16:30:11Z</dc:date>
    </item>
  </channel>
</rss>

