<?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 group by - add row for missing groups for each group in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/group-by-add-row-for-missing-groups-for-each-group/m-p/911052#M359255</link>
    <description>&lt;P&gt;New to SAS. I'm updating an old report. I need to reshape the data a certain way so it can be uploaded/consumed. Would like to avoid going higher up through the process to rectify this as it will probably change the values.&amp;nbsp;&lt;/P&gt;&lt;P&gt;In a nutshell, is there a way to insert a line with zero values for the variables, for groups missing a certain type.&amp;nbsp;&lt;/P&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="btbell_0-1704840474209.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/92411i66CA05AC42D91992/image-size/medium?v=v2&amp;amp;px=400" role="button" title="btbell_0-1704840474209.png" alt="btbell_0-1704840474209.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I.e. i want all vehicle type groups to have all 3 types (car, van, suv). at this point, it could even be a proc print step if that's possible.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks! Sorry for the brevity.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 09 Jan 2024 22:50:58 GMT</pubDate>
    <dc:creator>btbell</dc:creator>
    <dc:date>2024-01-09T22:50:58Z</dc:date>
    <item>
      <title>group by - add row for missing groups for each group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/group-by-add-row-for-missing-groups-for-each-group/m-p/911052#M359255</link>
      <description>&lt;P&gt;New to SAS. I'm updating an old report. I need to reshape the data a certain way so it can be uploaded/consumed. Would like to avoid going higher up through the process to rectify this as it will probably change the values.&amp;nbsp;&lt;/P&gt;&lt;P&gt;In a nutshell, is there a way to insert a line with zero values for the variables, for groups missing a certain type.&amp;nbsp;&lt;/P&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="btbell_0-1704840474209.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/92411i66CA05AC42D91992/image-size/medium?v=v2&amp;amp;px=400" role="button" title="btbell_0-1704840474209.png" alt="btbell_0-1704840474209.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I.e. i want all vehicle type groups to have all 3 types (car, van, suv). at this point, it could even be a proc print step if that's possible.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks! Sorry for the brevity.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 09 Jan 2024 22:50:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/group-by-add-row-for-missing-groups-for-each-group/m-p/911052#M359255</guid>
      <dc:creator>btbell</dc:creator>
      <dc:date>2024-01-09T22:50:58Z</dc:date>
    </item>
    <item>
      <title>Re: group by - add row for missing groups for each group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/group-by-add-row-for-missing-groups-for-each-group/m-p/911053#M359256</link>
      <description>&lt;P&gt;PROC MEANS in combination with the COMPLETETYPES and PRELOADFMT options should provide what you want. Check out this &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/proc/n1eseaucwkzo18n1nph31op5kkdt.htm#n1eseaucwkzo18n1nph31op5kkdt" target="_blank" rel="noopener"&gt;SAS doc example&lt;/A&gt;.&lt;/P&gt;</description>
      <pubDate>Tue, 09 Jan 2024 23:33:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/group-by-add-row-for-missing-groups-for-each-group/m-p/911053#M359256</guid>
      <dc:creator>SASKiwi</dc:creator>
      <dc:date>2024-01-09T23:33:53Z</dc:date>
    </item>
    <item>
      <title>Re: group by - add row for missing groups for each group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/group-by-add-row-for-missing-groups-for-each-group/m-p/911083#M359264</link>
      <description>&lt;P&gt;&lt;A href="https://blogs.sas.com/content/iml/2024/01/08/unobserved-levels-categorical.html" target="_blank" rel="noopener"&gt;https://blogs.sas.com/content/iml/2024/01/08/unobserved-levels-categorical.html&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;That would be better if you post some data.&lt;BR /&gt;&lt;BR /&gt;Here is an example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data have;
set sashelp.class;
run;


proc sql;
create table want as
select a.*,coalesce(weight,0) as weight,coalesce(height,0) as height
from (select * from (select distinct sex from have),(select distinct age from have)) as a
natural left join
(select sex,age,sum(weight) as weight from have group by sex,age)
natural left join
(select sex,age,sum(height) as height from have group by sex,age)
;
quit;&lt;/PRE&gt;</description>
      <pubDate>Wed, 10 Jan 2024 09:31:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/group-by-add-row-for-missing-groups-for-each-group/m-p/911083#M359264</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2024-01-10T09:31:36Z</dc:date>
    </item>
    <item>
      <title>Re: group by - add row for missing groups for each group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/group-by-add-row-for-missing-groups-for-each-group/m-p/911149#M359291</link>
      <description>&lt;P&gt;Thanks for the responses so far. Let me explain this another way. The spreadsheet visual I included before is accurate of the data. It's a comprehensive table of obs that has been grouped by vehicle type. What i'm trying to add is&amp;nbsp;&lt;/P&gt;&lt;P&gt;'for any grouping (car_type) i want it to have (car, van, suv), if van is missing, insert it' so ALL groupings have car, van, suv - in that order. If the var values are empty, that's fine i can handle that with missing option.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 10 Jan 2024 14:56:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/group-by-add-row-for-missing-groups-for-each-group/m-p/911149#M359291</guid>
      <dc:creator>btbell</dc:creator>
      <dc:date>2024-01-10T14:56:54Z</dc:date>
    </item>
    <item>
      <title>Re: group by - add row for missing groups for each group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/group-by-add-row-for-missing-groups-for-each-group/m-p/911165#M359299</link>
      <description>I think the options you been provided are relevant for your use case. Let us know if you don'tunderstand how to apply them.&lt;BR /&gt;You could get more hands-on help if you provide with programming steps that creates the report data set.</description>
      <pubDate>Wed, 10 Jan 2024 16:34:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/group-by-add-row-for-missing-groups-for-each-group/m-p/911165#M359299</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2024-01-10T16:34:23Z</dc:date>
    </item>
    <item>
      <title>Re: group by - add row for missing groups for each group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/group-by-add-row-for-missing-groups-for-each-group/m-p/911203#M359314</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/453350"&gt;@btbell&lt;/a&gt;,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I had a similar (not exact) problem in the past and Community Experts (Ksharp) recommended to use SPARSE option in proc freq step which generates extra records for missing categories (per group) with 0 values.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Below is an example and hope this helps you to figure out your solution.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;*Create data HAVE; 
data have; 
	set sashelp.cars; 
	if upcase(substr(make, 1, 1)) in ('A' 'B' 'C' 'D' 'E') then make= 'Var1';
	else if upcase(substr(make, 1, 1)) in ('F' 'G' 'H' 'I' 'J') then make= 'Var2';
	else if upcase(substr(make, 1, 1)) in ('K' 'L' 'M' 'N' 'O') then make= 'Var3';
	else make= 'Var4';
run;

*Calculate frequency counts for all categories, and creating null records for missing categories;  
proc freq data= have noprint;
	tables origin*make*type/out=have_wide missing sparse;
run; 

*Transposing the results to get the desired table structure; 
proc sort data=have_wide; by origin type make; run; 
proc transpose data=have_wide out=want (drop=_:);
	by origin type;
	var count;
	id make; 
run; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;As you see in the result, we have all types per ORIGIN, even though it did not exist in the source data.&amp;nbsp;&lt;/P&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="Capture.PNG" style="width: 314px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/92437i1A760F37305EF79B/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture.PNG" alt="Capture.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 10 Jan 2024 23:32:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/group-by-add-row-for-missing-groups-for-each-group/m-p/911203#M359314</guid>
      <dc:creator>A_Kh</dc:creator>
      <dc:date>2024-01-10T23:32:59Z</dc:date>
    </item>
    <item>
      <title>Re: group by - add row for missing groups for each group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/group-by-add-row-for-missing-groups-for-each-group/m-p/911221#M359320</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/321371"&gt;@A_Kh&lt;/a&gt;&lt;/P&gt;&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13674"&gt;@LinusH&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13976"&gt;@SASKiwi&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for you all's help. I've probably made things more complicated by the fact that i'm so new to SAS, to say there's a gap in my knowledge at this point is an understatement. There are so many ways this could probably be handled so let me start with a different question based on the data below - can i get all obs's numerical values onto one line (obs) in that order? The orig vars can be replaced and made to simply be var1...var18 ( i won't need the sec or id)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data have;
input sec $ id $ fr_m fr_f tot_;
cards;
06 M 10 8 0
06 D 0 12 120
06 To 8 18 20
06 PT 3 7 9
06 FT 53 41 9
06 To 7 79 13
;
run;&lt;/PRE&gt;&lt;P&gt;Want:&lt;/P&gt;&lt;P&gt;I need this out on a single line, e.g.&lt;/P&gt;&lt;P&gt;10 8 0&amp;nbsp;0 12 120&amp;nbsp;8 18 20&amp;nbsp;3 7 9&amp;nbsp;53 41 9&amp;nbsp;7 79 13&lt;/P&gt;&lt;P&gt;so my text file would be&lt;/P&gt;&lt;PRE&gt;data _null_;

	set have;
file "outfile.txt";

put @1 "xxxx" @5 "yyy" @7 "zzz" @11 var1 @16 var2 @21 var3 @26 var4 @31 var5 @36 var 6 @41 var 7 @46 var 8
	@51 var9 @56 var10 @61 var11 @66 var 12 @71 var 13 @76 var 14 @81 var 15 @86 var 
	@91 var 16 @96 var 17 @101 var 18 
run;&lt;/PRE&gt;&lt;P&gt;Thanks for your patience and any help is appreciated.&lt;/P&gt;</description>
      <pubDate>Thu, 11 Jan 2024 03:37:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/group-by-add-row-for-missing-groups-for-each-group/m-p/911221#M359320</guid>
      <dc:creator>btbell</dc:creator>
      <dc:date>2024-01-11T03:37:50Z</dc:date>
    </item>
    <item>
      <title>Re: group by - add row for missing groups for each group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/group-by-add-row-for-missing-groups-for-each-group/m-p/911233#M359323</link>
      <description>&lt;P&gt;So you want output to be a dataset or a txt file ?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input sec $ id $ fr_m fr_f tot_;
cards;
06 M 10 8 0
06 D 0 12 120
06 To 8 18 20
06 PT 3 7 9
06 FT 53 41 9
06 To 7 79 13
;
run;
proc iml;
use have;
read all var _num_ into x;
close;
want=rowvec(x);
create want from want;
append from want;
close;
quit;
proc export data=want outfile='c:\temp\outfile.txt' dbms=tab replace;run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 11 Jan 2024 05:51:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/group-by-add-row-for-missing-groups-for-each-group/m-p/911233#M359323</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2024-01-11T05:51:27Z</dc:date>
    </item>
    <item>
      <title>Re: group by - add row for missing groups for each group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/group-by-add-row-for-missing-groups-for-each-group/m-p/911303#M359361</link>
      <description>SAS community is holding my post for approval. not sure what that's about. anyway, i actually, got it working. i found my mistakes in the reply post i sent you earlier but hasn't been released. thanks!</description>
      <pubDate>Thu, 11 Jan 2024 17:14:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/group-by-add-row-for-missing-groups-for-each-group/m-p/911303#M359361</guid>
      <dc:creator>btbell</dc:creator>
      <dc:date>2024-01-11T17:14:17Z</dc:date>
    </item>
  </channel>
</rss>

