<?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: Add missing rows for several subgroups in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Add-missing-rows-for-several-subgroups/m-p/465220#M118657</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input diagnosis	$ age_group	$ number_patients;
cards;
A	0-4	3
A	10-15	2
A	5-9	15
A	16-20	8
B	16-20	1
C	5-9	1
D	0-4	1
D	16-20	6
D	5-9	2
E	0-4	1
;

data want;
if _n_=1 then do;
if 0 then set have;
  dcl hash H (dataset:'have') ;
   h.definekey  ("age_group") ;
   h.definedata ("age_group") ;
   h.definedone () ;
   declare hiter iter('h');
 dcl hash H1 () ;
   h1.definekey  ("age_group") ;
   h1.definedone () ;
 end;
h1.clear();
do until(last.diagnosis);
	set have;
	by diagnosis;
	if h.check()=0 and h1.num_items ne h.num_items then do; h1.add();output;end;
	if last.diagnosis then do;
	rc = iter.first();
	do while (rc = 0);
 	 if h1.check() ne 0 then do;call missing(number_patients); output;end;
  	rc = iter.next();
	end;
	end;
end;
drop rc;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 25 May 2018 20:33:40 GMT</pubDate>
    <dc:creator>novinosrin</dc:creator>
    <dc:date>2018-05-25T20:33:40Z</dc:date>
    <item>
      <title>Add missing rows for several subgroups</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-missing-rows-for-several-subgroups/m-p/465195#M118637</link>
      <description>&lt;P&gt;Hello community:)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Some background Information for my question:&lt;/P&gt;&lt;P&gt;There a five diagnosis: A,B,C,D,E and four age groups: 0-4, 5-9, 10-15, 16-20 and there is a numerical variable for the number of patients in each group&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Now i have the following problem. My table looks like this:&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;diagnosis&lt;/TD&gt;&lt;TD&gt;age_group&lt;/TD&gt;&lt;TD&gt;number_patients&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;A&lt;/TD&gt;&lt;TD&gt;0-4&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;A&lt;/TD&gt;&lt;TD&gt;10-15&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;A&lt;/TD&gt;&lt;TD&gt;5-9&lt;/TD&gt;&lt;TD&gt;15&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;A&lt;/TD&gt;&lt;TD&gt;16-20&lt;/TD&gt;&lt;TD&gt;8&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;B&lt;/TD&gt;&lt;TD&gt;16-20&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;C&lt;/TD&gt;&lt;TD&gt;5-9&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;D&lt;/TD&gt;&lt;TD&gt;0-4&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;D&lt;/TD&gt;&lt;TD&gt;16-20&lt;/TD&gt;&lt;TD&gt;6&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;D&lt;/TD&gt;&lt;TD&gt;5-9&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;E&lt;/TD&gt;&lt;TD&gt;0-4&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;and i want my table look like this (including all Information even if its missing and in the right order regarding the age age groups):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;diagnosis&lt;/TD&gt;&lt;TD&gt;age_group&lt;/TD&gt;&lt;TD&gt;number_patients&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;A&lt;/TD&gt;&lt;TD&gt;0-4&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;A&lt;/TD&gt;&lt;TD&gt;5-9&lt;/TD&gt;&lt;TD&gt;15&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;A&lt;/TD&gt;&lt;TD&gt;10-15&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;A&lt;/TD&gt;&lt;TD&gt;16-20&lt;/TD&gt;&lt;TD&gt;8&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;B&lt;/TD&gt;&lt;TD&gt;0-4&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;B&lt;/TD&gt;&lt;TD&gt;5-9&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;B&lt;/TD&gt;&lt;TD&gt;10-15&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;B&lt;/TD&gt;&lt;TD&gt;16-20&lt;/TD&gt;&lt;TD&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;E&lt;/TD&gt;&lt;TD&gt;16-20&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;.&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;i hope someone could help me or link me to an answer!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for your effort!&lt;/P&gt;&lt;P&gt;Dynamike&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 25 May 2018 19:11:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-missing-rows-for-several-subgroups/m-p/465195#M118637</guid>
      <dc:creator>Dynamike</dc:creator>
      <dc:date>2018-05-25T19:11:51Z</dc:date>
    </item>
    <item>
      <title>Re: Add missing rows for several subgroups</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-missing-rows-for-several-subgroups/m-p/465198#M118640</link>
      <description>&lt;P&gt;1. Use the SPARSE option within PROC FREQ to help fill out your tables&lt;/P&gt;
&lt;P&gt;2. Add another variable that can control the order of the age groups. It's a character variable so it sorts alphabetically, or see the sort options in PROC SORT that allow it to not sort alphabetically.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc freq data=have noprint;
table diagnosis*age_group/ out=filled_sparse sparse list;&lt;BR /&gt;weight number_patients;
run;

proc print data=filled_sparse;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;It essentailly does a cross join between the diagnosis and age_group so any entry will be included. If you're missing an age group for all levels this method will not work.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 25 May 2018 19:23:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-missing-rows-for-several-subgroups/m-p/465198#M118640</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-05-25T19:23:49Z</dc:date>
    </item>
    <item>
      <title>Re: Add missing rows for several subgroups</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-missing-rows-for-several-subgroups/m-p/465208#M118647</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA HAVE;
input diagnosis $ age_group $ number_patients;
DATALINES;
A 0-4 3
A 10-15 2
A 5-9 15
A 16-20 8
B 16-20 1
C 5-9 1
D 0-4 1
D 16-20 6
D 5-9 2
E 0-4 1
;
RUN;


data Categories;

FORMAT  Diagnosis $ 1.
		Age_group $ 8.;


DO Diagnosis = 'A','B','C','D','E';
	DO Age_Group = '0-4','5-9','10-15','16-20';
	OUTPUT;
	end;
end;

RUN;

Proc sql;

CREATE Table want AS
	SELECT cat.Diagnosis, cat.Age_group, h.number_patients
	FROM work.Categories AS Cat
	LEFT JOIN work.have AS h ON h.Diagnosis = cat.diagnosis
	AND h.Age_group = Cat.Age_group
	
;
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I am assuming you already have the result and that it is just a case of adding the missing values and having it all sorted. This achieves that although there are most likely better answers out there.&lt;/P&gt;</description>
      <pubDate>Fri, 25 May 2018 20:03:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-missing-rows-for-several-subgroups/m-p/465208#M118647</guid>
      <dc:creator>DanielLangley</dc:creator>
      <dc:date>2018-05-25T20:03:06Z</dc:date>
    </item>
    <item>
      <title>Re: Add missing rows for several subgroups</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-missing-rows-for-several-subgroups/m-p/465209#M118648</link>
      <description>&lt;P&gt;Something like this would work:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile datalines dlm='09'x missover;
input diagnosis :$	age_group :$	number_patients;
datalines;
A	0-4	3
A	10-15	2
A	5-9	15
A	16-20	8
B	16-20	1
C	5-9	1
D	0-4	1
D	16-20	6
D	5-9	2
E	0-4	1
;
run;

proc sql;
select a.*,b.number_patients
from (select * from (select distinct diagnosis from have),
	 (select distinct age_group from have)) a
left join	have b on a.diagnosis=b.diagnosis and a.age_group=b.age_group

;

quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 25 May 2018 19:58:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-missing-rows-for-several-subgroups/m-p/465209#M118648</guid>
      <dc:creator>SuryaKiran</dc:creator>
      <dc:date>2018-05-25T19:58:02Z</dc:date>
    </item>
    <item>
      <title>Re: Add missing rows for several subgroups</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-missing-rows-for-several-subgroups/m-p/465218#M118655</link>
      <description>&lt;P&gt;Do you actually want to add records to a data set or just display the existing data in the desired format?&lt;/P&gt;</description>
      <pubDate>Fri, 25 May 2018 20:30:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-missing-rows-for-several-subgroups/m-p/465218#M118655</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-05-25T20:30:01Z</dc:date>
    </item>
    <item>
      <title>Re: Add missing rows for several subgroups</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-missing-rows-for-several-subgroups/m-p/465220#M118657</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input diagnosis	$ age_group	$ number_patients;
cards;
A	0-4	3
A	10-15	2
A	5-9	15
A	16-20	8
B	16-20	1
C	5-9	1
D	0-4	1
D	16-20	6
D	5-9	2
E	0-4	1
;

data want;
if _n_=1 then do;
if 0 then set have;
  dcl hash H (dataset:'have') ;
   h.definekey  ("age_group") ;
   h.definedata ("age_group") ;
   h.definedone () ;
   declare hiter iter('h');
 dcl hash H1 () ;
   h1.definekey  ("age_group") ;
   h1.definedone () ;
 end;
h1.clear();
do until(last.diagnosis);
	set have;
	by diagnosis;
	if h.check()=0 and h1.num_items ne h.num_items then do; h1.add();output;end;
	if last.diagnosis then do;
	rc = iter.first();
	do while (rc = 0);
 	 if h1.check() ne 0 then do;call missing(number_patients); output;end;
  	rc = iter.next();
	end;
	end;
end;
drop rc;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 25 May 2018 20:33:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-missing-rows-for-several-subgroups/m-p/465220#M118657</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-05-25T20:33:40Z</dc:date>
    </item>
  </channel>
</rss>

