<?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: Inserting row with retained data if missing from set in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Inserting-row-with-retained-data-if-missing-from-set/m-p/599935#M173285</link>
    <description>&lt;P&gt;There is no problem with making a template dataset and merging it back.&lt;/P&gt;
&lt;P&gt;Perhaps you just haven't explained what the rules for the template dataset is?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql ;
create table skeleton as
  select distinct id,type,color,4 as Quarter
  from testdata
  order by id,type,color,quarter
;
quit;

data want;
  merge skeleton testdata;
  by id type color quarter;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Mon, 28 Oct 2019 21:02:34 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2019-10-28T21:02:34Z</dc:date>
    <item>
      <title>Inserting row with retained data if missing from set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Inserting-row-with-retained-data-if-missing-from-set/m-p/599931#M173281</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; data WORK.TestData;
   infile datalines truncover;
   input ID:$8. Type:$1. Color:$32. Quarter:32. Score:32.;
 datalines;
 A11 1 G 1 99
 A11 1 G 2 98
 A11 1 G 3 100
 A11 2 B 1 97
 A11 2 B 2 90
 A11 2 B 3 90
 A12 1 G 1 99
 A12 1 G 2 88
 A12 1 G 3 48
 A12 1 G 4 59
 A13 1 G 1 99
 A13 1 G 2 88
 A13 1 G 3 48
;;;;;;;;;;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;So, in this data or data like it, i need a Quarter 4 represented for each person (unique ID) and each "type".&amp;nbsp; Even if the score would be empty I need to insert a row with a Quarter 4 and the above data to match it (again score as missing is fine).&amp;nbsp; This was problematic with creating an extra data set and merging back in because i had 2 "types" for one person in the data set. A PROC SQL would only merge in the top most Type of the Q4 data.&amp;nbsp; A retain statement was able to copy back in the data, but again, only for the first Type if more than one was present.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thoughts are most appreciated.&lt;/P&gt;</description>
      <pubDate>Mon, 28 Oct 2019 20:29:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Inserting-row-with-retained-data-if-missing-from-set/m-p/599931#M173281</guid>
      <dc:creator>altatunc</dc:creator>
      <dc:date>2019-10-28T20:29:10Z</dc:date>
    </item>
    <item>
      <title>Re: Inserting row with retained data if missing from set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Inserting-row-with-retained-data-if-missing-from-set/m-p/599932#M173282</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
 data WORK.TestData;
   infile datalines truncover;
   input ID:$8. Type:$1. Color:$32. Quarter:32. Score:32.;
 datalines;
 A11 1 G 1 99
 A11 1 G 2 98
 A11 1 G 3 100
 A11 2 B 1 97
 A11 2 B 2 90
 A11 2 B 3 90
 A12 1 G 1 99
 A12 1 G 2 88
 A12 1 G 3 48
 A12 1 G 4 59
 A13 1 G 1 99
 A13 1 G 2 88
 A13 1 G 3 48
;;;;;;;;;;

data want;
 do until(last.type);
  set testdata;
  by id type;
  output;
 end;
 if Quarter ne  4 then do Quarter=Quarter+1 to 4;
 call missing(score);
 output;
 end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 28 Oct 2019 20:37:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Inserting-row-with-retained-data-if-missing-from-set/m-p/599932#M173282</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-10-28T20:37:20Z</dc:date>
    </item>
    <item>
      <title>Re: Inserting row with retained data if missing from set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Inserting-row-with-retained-data-if-missing-from-set/m-p/599934#M173284</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; data WORK.TestData;
   infile datalines truncover;
   input ID:$8. Type:$1. Color:$32. Quarter:32. Score:32.;
 datalines;
 A11 1 G 1 99
 A11 1 G 2 98
 A11 1 G 3 100
 A11 2 B 1 97
 A11 2 B 2 90
 A11 2 B 3 90
 A12 1 G 1 99
 A12 1 G 2 88
 A12 1 G 3 48
 A12 1 G 4 59
 A13 1 G 1 99
 A13 1 G 2 88
 A13 1 G 3 48
;;;;;;;;;;
run;


data want;
set WORK.TestData;
by Id type;
output;

if last.type then do;
	do while(quarter lt 4);
			Quarter+1;
			output;
		end;
end;

run;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 28 Oct 2019 21:20:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Inserting-row-with-retained-data-if-missing-from-set/m-p/599934#M173284</guid>
      <dc:creator>r_behata</dc:creator>
      <dc:date>2019-10-28T21:20:19Z</dc:date>
    </item>
    <item>
      <title>Re: Inserting row with retained data if missing from set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Inserting-row-with-retained-data-if-missing-from-set/m-p/599935#M173285</link>
      <description>&lt;P&gt;There is no problem with making a template dataset and merging it back.&lt;/P&gt;
&lt;P&gt;Perhaps you just haven't explained what the rules for the template dataset is?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql ;
create table skeleton as
  select distinct id,type,color,4 as Quarter
  from testdata
  order by id,type,color,quarter
;
quit;

data want;
  merge skeleton testdata;
  by id type color quarter;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 28 Oct 2019 21:02:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Inserting-row-with-retained-data-if-missing-from-set/m-p/599935#M173285</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-10-28T21:02:34Z</dc:date>
    </item>
    <item>
      <title>Re: Inserting row with retained data if missing from set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Inserting-row-with-retained-data-if-missing-from-set/m-p/599941#M173287</link>
      <description>&lt;P&gt;Or SPARSE in proc freq is a beauty&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data WORK.TestData;
   infile datalines truncover;
   input ID:$8. Type:$1. Color:$32. Quarter:32. Score:32.;
 datalines;
 A11 1 G 1 99
 A11 1 G 2 98
 A11 1 G 3 100
 A11 2 B 1 97
 A11 2 B 2 90
 A11 2 B 3 90
 A12 1 G 1 99
 A12 1 G 2 88
 A12 1 G 3 48
 A12 1 G 4 59
 A13 1 G 1 99
 A13 1 G 2 88
 A13 1 G 3 48
;;;;;;;;;;

/*Get all combinations*/
proc freq data=testdata noprint;
tables id*type*quarter/out=temp(keep=id type quarter) sparse;
run;
/*Merge with SQL*/
proc sql;
create table want as
select a.*,b.score
from temp a left join testdata b
on a.id=b.id and a.type=b.type and a.quarter=b.quarter
order by a.id,a.type,a.quarter;
quit;
/*or Merge with datastep*/
data want;
merge temp testdata;
by id type quarter;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 28 Oct 2019 21:35:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Inserting-row-with-retained-data-if-missing-from-set/m-p/599941#M173287</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-10-28T21:35:24Z</dc:date>
    </item>
    <item>
      <title>Re: Inserting row with retained data if missing from set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Inserting-row-with-retained-data-if-missing-from-set/m-p/599957#M173294</link>
      <description>&lt;P&gt;Thank you! I am not sure what my merge had wrong but I suspect it was because i used a sql full join which only choose the first option and moved on.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 29 Oct 2019 00:55:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Inserting-row-with-retained-data-if-missing-from-set/m-p/599957#M173294</guid>
      <dc:creator>altatunc</dc:creator>
      <dc:date>2019-10-29T00:55:46Z</dc:date>
    </item>
    <item>
      <title>Re: Inserting row with retained data if missing from set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Inserting-row-with-retained-data-if-missing-from-set/m-p/599958#M173295</link>
      <description>&lt;P&gt;Thank you!&amp;nbsp; This seems to work with the test data and i am sure it will with the real data.&lt;/P&gt;</description>
      <pubDate>Tue, 29 Oct 2019 00:56:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Inserting-row-with-retained-data-if-missing-from-set/m-p/599958#M173295</guid>
      <dc:creator>altatunc</dc:creator>
      <dc:date>2019-10-29T00:56:19Z</dc:date>
    </item>
  </channel>
</rss>

