<?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: Merging observations with sub-key in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Merging-observations-with-sub-key/m-p/599004#M172825</link>
    <description>&lt;P&gt;Create a temporary sub-group, and use that to sum up the values:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	input num_fam num_sub val1 val2;
	cards;
123 1 75 0
123 2 0 12
124 1 14 0
124 2 0 5
125 1 10 0
125 2 0 10
125 3 16 0
125 4 0 10
126 1 14 0
;
run;

data int;
set have;
by num_fam;
retain count grp;
if first.num_fam then do;
  count = 1;
  grp = 0;
end;
else do;
  count + 1;
  if mod(count,2) = 1 then grp + 1;
end;
drop count;
run;

proc sql;
create table want (drop=grp) as
select num_fam, grp, min(num_sub) as num_sub, sum(val1) as val1, sum(val2) as val2
from int
group by num_fam, grp;
quit;

proc print data=want noobs;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;num_fam    num_sub    val1    val2

  123         1        75      12 
  124         1        14       5 
  125         1        10      10 
  125         3        16      10 
  126         1        14       0 
&lt;/PRE&gt;</description>
    <pubDate>Thu, 24 Oct 2019 12:41:06 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2019-10-24T12:41:06Z</dc:date>
    <item>
      <title>Merging observations with sub-key</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-observations-with-sub-key/m-p/598989#M172818</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a dataset with observations that need to be merged so that obs1 and obs2 are together, obs3 and obs4 together etc etc.&lt;/P&gt;
&lt;P&gt;Each observation has a two numbers, the "family" id and the sub id.&lt;/P&gt;
&lt;P&gt;For example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;have data:&lt;/P&gt;
&lt;P&gt;family&amp;nbsp; &amp;nbsp; &amp;nbsp;sub&amp;nbsp; &amp;nbsp; &amp;nbsp;val1&amp;nbsp; &amp;nbsp; &amp;nbsp;val2&lt;/P&gt;
&lt;P&gt;123&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 15&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&lt;/P&gt;
&lt;P&gt;123&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;12&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;wanted data:&lt;/P&gt;
&lt;P&gt;family&amp;nbsp; &amp;nbsp; sub&amp;nbsp; &amp;nbsp; val1&amp;nbsp; &amp;nbsp; &amp;nbsp;val2&lt;/P&gt;
&lt;P&gt;123&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 15&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 12&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Basically observations need to be brought back to the first. They always come in pairs, so my idea was checking sub % 2 if you get what I mean, but I don"t know how to proceed.&amp;nbsp; Note that each family can have more than two lines, it can go up to 4 and they don't always have a 2nd observation alongside it. The only indicator that says that they need to be combined is that val2 is different than 0.&lt;/P&gt;
&lt;P&gt;Here's the SAS code for the have and want data:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	input num_fam num_sub val1 val2;
	cards;
123 1 75 0
123 2 0 12
124 1 14 0
124 2 0 5
125 1 10 0
125 2 0 10
125 3 16 0
125 4 0 10
126 1 14 0
;
run;

data want;
	input num_fam num_sub val1 val2;
	cards;
123 1 75 12
124 1 14 5
125 1 10 10
125 3 16 10
126 1 14 0
;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 24 Oct 2019 12:04:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-observations-with-sub-key/m-p/598989#M172818</guid>
      <dc:creator>polpel</dc:creator>
      <dc:date>2019-10-24T12:04:56Z</dc:date>
    </item>
    <item>
      <title>Re: Merging observations with sub-key</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-observations-with-sub-key/m-p/599000#M172822</link>
      <description>&lt;P&gt;One way&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	input num_fam num_sub val1 val2;
	cards;
123 1 75 0
123 2 0 12
124 1 14 0
124 2 0 5
125 1 10 0
125 2 0 10
125 3 16 0
125 4 0 10
126 1 14 0
;
run;

data want(drop=_:);
    do until (last.num_fam);
        set have;
        by num_fam;
        _val1=max(_val1, val1);
        _val2=max(_val2, val2);
    end;
    val1=_val1;
    val2=_val2;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;num_fam  num_sub  val1  val2;
123      2        75    12
124      2        14    5
125      4        16    10
126      1        14    0&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 24 Oct 2019 12:33:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-observations-with-sub-key/m-p/599000#M172822</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-10-24T12:33:24Z</dc:date>
    </item>
    <item>
      <title>Re: Merging observations with sub-key</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-observations-with-sub-key/m-p/599001#M172823</link>
      <description>&lt;P&gt;Missed the last requirement, when reading want and have, so the following step might not produce the required result:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   set have(rename=(
         num_sub = _sub
         val1 = _val1
         val2 = _val2
   ));
   by num_fam;

   if 0 then set have;
   retain num_sub val1 val2;

   if mod(_sub, 2) = 1 then do;
      num_sub = _sub;
      val1 = _val1;
   end;

   if last.num_fam or mod(_sub, 2) = 0 then do;
      val2 = _val2;
      output;
   end;

   drop _:;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 24 Oct 2019 12:32:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-observations-with-sub-key/m-p/599001#M172823</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2019-10-24T12:32:26Z</dc:date>
    </item>
    <item>
      <title>Re: Merging observations with sub-key</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-observations-with-sub-key/m-p/599004#M172825</link>
      <description>&lt;P&gt;Create a temporary sub-group, and use that to sum up the values:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	input num_fam num_sub val1 val2;
	cards;
123 1 75 0
123 2 0 12
124 1 14 0
124 2 0 5
125 1 10 0
125 2 0 10
125 3 16 0
125 4 0 10
126 1 14 0
;
run;

data int;
set have;
by num_fam;
retain count grp;
if first.num_fam then do;
  count = 1;
  grp = 0;
end;
else do;
  count + 1;
  if mod(count,2) = 1 then grp + 1;
end;
drop count;
run;

proc sql;
create table want (drop=grp) as
select num_fam, grp, min(num_sub) as num_sub, sum(val1) as val1, sum(val2) as val2
from int
group by num_fam, grp;
quit;

proc print data=want noobs;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;num_fam    num_sub    val1    val2

  123         1        75      12 
  124         1        14       5 
  125         1        10      10 
  125         3        16      10 
  126         1        14       0 
&lt;/PRE&gt;</description>
      <pubDate>Thu, 24 Oct 2019 12:41:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-observations-with-sub-key/m-p/599004#M172825</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-10-24T12:41:06Z</dc:date>
    </item>
    <item>
      <title>Re: Merging observations with sub-key</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-observations-with-sub-key/m-p/599007#M172827</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	input num_fam num_sub val1 val2;
	cards;
123 1 75 0
123 2 0 12
124 1 14 0
124 2 0 5
125 1 10 0
125 2 0 10
125 3 16 0
125 4 0 10
126 1 14 0
;
run;
data want;
 merge have have(keep=val1 val2
rename=(val1=_val1 val2=_val2) firstobs=2);
val1=sum(val1,_val1);
val2=sum(val2,_val2);
if mod(_n_,2)=1;
drop _:;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 24 Oct 2019 12:44:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-observations-with-sub-key/m-p/599007#M172827</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2019-10-24T12:44:23Z</dc:date>
    </item>
    <item>
      <title>Re: Merging observations with sub-key</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-observations-with-sub-key/m-p/599011#M172829</link>
      <description>&lt;P&gt;Your have data violates your rule, as num_fam=126 has only one observation:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	input num_fam num_sub val1 val2;
	cards;
123 1 75 0
123 2 0 12
124 1 14 0
124 2 0 5
125 1 10 0
125 2 0 10
125 3 16 0
125 4 0 10
126 1 14 0
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If that happens in the middle of the dataset, it might invalidate solutions that expect an even number of observations for &lt;EM&gt;every&lt;/EM&gt; num_fam.&lt;/P&gt;</description>
      <pubDate>Thu, 24 Oct 2019 12:50:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-observations-with-sub-key/m-p/599011#M172829</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-10-24T12:50:14Z</dc:date>
    </item>
  </channel>
</rss>

