<?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: aggregate - group by in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/aggregate-group-by/m-p/899376#M355499</link>
    <description>amazing, that was exactly what I was looking for. tried transpose but did not think of the double transpose way! thank you very much!</description>
    <pubDate>Fri, 20 Oct 2023 13:14:14 GMT</pubDate>
    <dc:creator>KoVa</dc:creator>
    <dc:date>2023-10-20T13:14:14Z</dc:date>
    <item>
      <title>aggregate - group by</title>
      <link>https://communities.sas.com/t5/SAS-Programming/aggregate-group-by/m-p/899367#M355492</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am working on conversion of a table with many missing values that I need to group to one.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;each id has for each variable either missing or a filled in value. so only 2 options, not more.&lt;/P&gt;
&lt;P&gt;table looks like this&lt;/P&gt;
&lt;P&gt;have&lt;/P&gt;
&lt;PRE&gt;ID	FLG1	FLG2	FLG3	FLG4	FLG5	FLG6	FLG7	FLG8	FLG9
1		
1 	X
1		X	
2			X
2	X			
2				X
2					SEG1
3						SEG4
3	X&lt;/PRE&gt;
&lt;P&gt;want&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;ID	FLG1	FLG2	FLG3	FLG4	FLG5	FLG6	FLG7	FLG8	FLG9
1	X	X
2	X		X	X	SEG1
3	X					SEG4&lt;/PRE&gt;
&lt;P&gt;I started with using a dummy value to copy the value to other rows using retain,&lt;/P&gt;
&lt;P&gt;but it seems too tedious, since I have to do it for each variable (even tough I could put in a macro).&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;%macro row_copy(data_in,data_out,var1,var2);

%sort(&amp;amp;data_in,&amp;amp;var1 descending &amp;amp;var2);
data &amp;amp;data_out;

set &amp;amp;data_in;

by &amp;amp;var1;

retain dummyvar;

if first.&amp;amp;var1 then dummyvar = &amp;amp;var2;

if missing(&amp;amp;var2) then &amp;amp;var2 = dummyvar;

drop dummyvar;

run;

%mend row_copy;&lt;/PRE&gt;
&lt;P&gt;than I would repeat the macro for each value,&lt;/P&gt;
&lt;P&gt;for instance if I would to do it for this table&lt;/P&gt;
&lt;PRE&gt;%row_copy(have,want1,ID,flg1);
%row_copy(want2,want1,ID,flg2);&lt;/PRE&gt;
&lt;P&gt;etc...and repeat for all flags, this gives a table still with duplicate rows by ID, but no more missing values.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;than I can just end with a group by to get the final result.&lt;/P&gt;
&lt;P&gt;but I feel like there is an easier way that I am missing (in one step and not 10)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 20 Oct 2023 12:33:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/aggregate-group-by/m-p/899367#M355492</guid>
      <dc:creator>KoVa</dc:creator>
      <dc:date>2023-10-20T12:33:40Z</dc:date>
    </item>
    <item>
      <title>Re: aggregate - group by</title>
      <link>https://communities.sas.com/t5/SAS-Programming/aggregate-group-by/m-p/899369#M355494</link>
      <description>&lt;P&gt;You would make your programming a huge amount simpler if instead of X you use the number (not character) 1, and if instead of SEG1 use the number&amp;nbsp;(not character) 1, and if instead of SEG4, you use the number&amp;nbsp;(not character) 4.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then, the coding is very simple&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary data=have nway;
     class id;
     var flg1-flg9;
     output out=want max=;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So, convert your X, SEG1 and SEG4 to numeric values.&lt;/P&gt;</description>
      <pubDate>Fri, 20 Oct 2023 12:43:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/aggregate-group-by/m-p/899369#M355494</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-10-20T12:43:59Z</dc:date>
    </item>
    <item>
      <title>Re: aggregate - group by</title>
      <link>https://communities.sas.com/t5/SAS-Programming/aggregate-group-by/m-p/899373#M355497</link>
      <description>&lt;P&gt;thx for the suggestion, but unfortunately, half of them are X, the others not the same string wise. you could have values like PINK, unknown, blue, outdated,...&lt;/P&gt;
&lt;P&gt;I would have to make a huge referential table, and also the values tend to change as well (I need to run this routine daily), so I would need to change everything when a new value of a certain flag is released.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also the names aren't really flg1 etc, but more like f.e. segment, target, scope,...so i'd need to convert that as well.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 20 Oct 2023 12:49:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/aggregate-group-by/m-p/899373#M355497</guid>
      <dc:creator>KoVa</dc:creator>
      <dc:date>2023-10-20T12:49:43Z</dc:date>
    </item>
    <item>
      <title>Re: aggregate - group by</title>
      <link>https://communities.sas.com/t5/SAS-Programming/aggregate-group-by/m-p/899374#M355498</link>
      <description>&lt;P&gt;Ok, how about the old double transpose method?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data fake;
    infile cards truncover dlm=',' dsd;
    input id (flg1-flg4)($) ;
    cards;
1,X
1, ,X
1, , ,SEG1
2, ,X
2, , , ,FROG
;
proc transpose data=fake out=fake2;
    by id;
    var flg1-flg4;
run;
data want_transposed;
    set fake2;
    final=coalescec(of col:);
run;
proc transpose data=want_transposed out=want;
    by id;
    var final;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 20 Oct 2023 13:06:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/aggregate-group-by/m-p/899374#M355498</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-10-20T13:06:02Z</dc:date>
    </item>
    <item>
      <title>Re: aggregate - group by</title>
      <link>https://communities.sas.com/t5/SAS-Programming/aggregate-group-by/m-p/899376#M355499</link>
      <description>amazing, that was exactly what I was looking for. tried transpose but did not think of the double transpose way! thank you very much!</description>
      <pubDate>Fri, 20 Oct 2023 13:14:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/aggregate-group-by/m-p/899376#M355499</guid>
      <dc:creator>KoVa</dc:creator>
      <dc:date>2023-10-20T13:14:14Z</dc:date>
    </item>
    <item>
      <title>Re: aggregate - group by</title>
      <link>https://communities.sas.com/t5/SAS-Programming/aggregate-group-by/m-p/899519#M355547</link>
      <description>&lt;P&gt;Did I miss something ?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data fake;
    infile cards truncover dlm=',' dsd;
    input id (flg1-flg4)($) ;
    cards;
1,X
1, ,X
1, , ,SEG1
2, ,X
2, , , ,FROG
;

data want;
 update fake(obs=0) fake;
 by id;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 21 Oct 2023 10:17:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/aggregate-group-by/m-p/899519#M355547</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2023-10-21T10:17:23Z</dc:date>
    </item>
  </channel>
</rss>

