<?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: combining two data sets conditionally in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/combining-two-data-sets-conditionally/m-p/52237#M11014</link>
    <description>Thanks for the help Cynthia.  Yes, I want only some obs from the second dataset, but the obs that I want do not have a consistent value for the variable I am selecting on.  I only want the obs from the second dataset where variablex (famtrac) in the second dataset matches any value of variablex (famtrac) in the first dataset...&lt;BR /&gt;
&lt;BR /&gt;
In case anyone is also trying to figure out how to do this, here's what I finally came up with:&lt;BR /&gt;
&lt;BR /&gt;
proc sql;&lt;BR /&gt;
create table sql.fam6508&lt;BR /&gt;
	like sql.cps65plus08a;&lt;BR /&gt;
proc sql;&lt;BR /&gt;
insert into sql.fam6508&lt;BR /&gt;
	select * from sql.cps65plus08a  **the primary data set**&lt;BR /&gt;
	union all&lt;BR /&gt;
	select * from sql.cps65under08  **secondary data set**&lt;BR /&gt;
		where famtrac in &lt;BR /&gt;
		(select famtrac from sql.cps65plus08a);</description>
    <pubDate>Wed, 08 Oct 2008 18:38:46 GMT</pubDate>
    <dc:creator>deleted_user</dc:creator>
    <dc:date>2008-10-08T18:38:46Z</dc:date>
    <item>
      <title>combining two data sets conditionally</title>
      <link>https://communities.sas.com/t5/SAS-Programming/combining-two-data-sets-conditionally/m-p/52231#M11008</link>
      <description>Hi, I am trying to combine two data sets with the same variables conditionally.  I only want to combine observations from the second data set that have the same value as a variablex (famtrac below) in the primary data set.  Variablex has the same name &amp;amp; same format in both data sets.  I tried using the set statement with in= and an if statement, but that didn't work:&lt;BR /&gt;
data all65plus08;&lt;BR /&gt;
set selena.cps65plus08a (in=eldfam) selena.cps65under08;&lt;BR /&gt;
by famtrac;&lt;BR /&gt;
if eldfam;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
There are duplicate observations in the secondary data set that meet the conditions I specified and I *do* want these duplicates to be added to the new combined data set.&lt;BR /&gt;
&lt;BR /&gt;
Any help would be appreciated!</description>
      <pubDate>Mon, 06 Oct 2008 18:52:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/combining-two-data-sets-conditionally/m-p/52231#M11008</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2008-10-06T18:52:47Z</dc:date>
    </item>
    <item>
      <title>Re: combining two data sets conditionally</title>
      <link>https://communities.sas.com/t5/SAS-Programming/combining-two-data-sets-conditionally/m-p/52232#M11009</link>
      <description>You are using SET (interleave observations) with a BY.  Do you mean to use a MERGE with a BY, in order to "combine" your two files?  Do consider the impact of the MERGE operation with same-named variables in the two (or more) files on the MERGE statement.  Have a look at the SAS support website  &lt;A href="http://support.sas.com/" target="_blank"&gt;http://support.sas.com/&lt;/A&gt; and the Product Documentation for details about the Data step's MERGE statement processing.&lt;BR /&gt;
&lt;BR /&gt;
Scott Barry&lt;BR /&gt;
SBBWorks, Inc.</description>
      <pubDate>Mon, 06 Oct 2008 19:16:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/combining-two-data-sets-conditionally/m-p/52232#M11009</guid>
      <dc:creator>sbb</dc:creator>
      <dc:date>2008-10-06T19:16:40Z</dc:date>
    </item>
    <item>
      <title>Re: combining two data sets conditionally</title>
      <link>https://communities.sas.com/t5/SAS-Programming/combining-two-data-sets-conditionally/m-p/52233#M11010</link>
      <description>I don't want to merge the data sets, I need to interleave them (the end result would be a data set with the observations from the primary data set plus all observations from the secondary data set that have a value for famtrac that occurs for some observation in the primary data set).&lt;BR /&gt;
&lt;BR /&gt;
I want to interleave observations, but I want to specify which observations from the secondary data set will be included in the interleaved data set...</description>
      <pubDate>Mon, 06 Oct 2008 19:34:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/combining-two-data-sets-conditionally/m-p/52233#M11010</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2008-10-06T19:34:02Z</dc:date>
    </item>
    <item>
      <title>Re: combining two data sets conditionally</title>
      <link>https://communities.sas.com/t5/SAS-Programming/combining-two-data-sets-conditionally/m-p/52234#M11011</link>
      <description>Then you need to add an IN= for the second dataset on the SET (not the first one), and use IF/THEN logic to conditionally output obs contributed from the second dataset.&lt;BR /&gt;
&lt;BR /&gt;
Scott Barry&lt;BR /&gt;
SBBWorks, Inc.</description>
      <pubDate>Mon, 06 Oct 2008 21:01:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/combining-two-data-sets-conditionally/m-p/52234#M11011</guid>
      <dc:creator>sbb</dc:creator>
      <dc:date>2008-10-06T21:01:06Z</dc:date>
    </item>
    <item>
      <title>Re: combining two data sets conditionally</title>
      <link>https://communities.sas.com/t5/SAS-Programming/combining-two-data-sets-conditionally/m-p/52235#M11012</link>
      <description>And it appears that you will also need to keep track of the prior first DSN contribution, using a RETAIN and assigning a temporary (different named) variable.&lt;BR /&gt;
&lt;BR /&gt;
Scott Barry&lt;BR /&gt;
SBBWorks, Inc.</description>
      <pubDate>Mon, 06 Oct 2008 21:03:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/combining-two-data-sets-conditionally/m-p/52235#M11012</guid>
      <dc:creator>sbb</dc:creator>
      <dc:date>2008-10-06T21:03:30Z</dc:date>
    </item>
    <item>
      <title>Re: combining two data sets conditionally</title>
      <link>https://communities.sas.com/t5/SAS-Programming/combining-two-data-sets-conditionally/m-p/52236#M11013</link>
      <description>Hi:&lt;BR /&gt;
   You are on the right track for interleaving, however, your IN= variable and your subsetting IF are limiting the new dataset to only those observations that come from selena.cps65plus08a -- which doesn't sound like what you want. &lt;BR /&gt;
               &lt;BR /&gt;
  You might need to put an IN= on the secondary dataset as well, and then tailor your IF statement accordingly. If I understand you correctly, you want EVERY obs from the first dataset, but only SOME obs from the second dataset??&lt;BR /&gt;
                    &lt;BR /&gt;
  Consider this data (Name, Present_type, Amtspend are my variables. I'm also making a variable called FROMFILE so that the final output shows which file contributed the interleaved observation):&lt;BR /&gt;
[pre]&lt;BR /&gt;
data one;&lt;BR /&gt;
  length name present_type $12;&lt;BR /&gt;
  infile datalines;&lt;BR /&gt;
  input name $ present_type $ amtspend;&lt;BR /&gt;
  fromfile = 'one';&lt;BR /&gt;
return;&lt;BR /&gt;
datalines;&lt;BR /&gt;
alan anniversary 15&lt;BR /&gt;
alan birthday 10&lt;BR /&gt;
carl anniversary 15&lt;BR /&gt;
edna birthday 20&lt;BR /&gt;
;&lt;BR /&gt;
run;&lt;BR /&gt;
                     &lt;BR /&gt;
data two;&lt;BR /&gt;
  length name present_type $12;&lt;BR /&gt;
  infile datalines;&lt;BR /&gt;
  input name $ present_type $ amtspend;&lt;BR /&gt;
  fromfile = 'two';&lt;BR /&gt;
return;&lt;BR /&gt;
datalines;&lt;BR /&gt;
alan christmas 25&lt;BR /&gt;
bob anniversary 10&lt;BR /&gt;
bob birthday 15&lt;BR /&gt;
carl birthday 10&lt;BR /&gt;
edna christmas 15&lt;BR /&gt;
;&lt;BR /&gt;
run;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
                                            &lt;BR /&gt;
For this data, I want ALL the obs from dataset ONE and everybody from dataset TWO except poor Carl -- he only gets an anniversary present. So this is the output I want:&lt;BR /&gt;
[pre]                      &lt;BR /&gt;
                present_&lt;BR /&gt;
Obs    name       type        amtspend    fromfile&lt;BR /&gt;
&lt;BR /&gt;
 1     alan    anniversary       15         one&lt;BR /&gt;
 2     alan    birthday          10         one&lt;BR /&gt;
 3     alan    christmas         25         two&lt;BR /&gt;
 4     bob     anniversary       10         two&lt;BR /&gt;
 5     bob     birthday          15         two&lt;BR /&gt;
 6     carl    anniversary       15         one&lt;BR /&gt;
 7     edna    birthday          20         one&lt;BR /&gt;
 8     edna    christmas         15         two&lt;BR /&gt;
                     &lt;BR /&gt;
[/pre] &lt;BR /&gt;
                                                                         &lt;BR /&gt;
Created by THIS program:    &lt;BR /&gt;
[pre]&lt;BR /&gt;
data three;&lt;BR /&gt;
  set one(in=inone)&lt;BR /&gt;
      two(in=intwo);&lt;BR /&gt;
  by name;&lt;BR /&gt;
  if inone or&lt;BR /&gt;
     (intwo and name ne 'carl') then output;&lt;BR /&gt;
run;&lt;BR /&gt;
                           &lt;BR /&gt;
ods listing;&lt;BR /&gt;
proc print data=three;&lt;BR /&gt;
  title 'three';&lt;BR /&gt;
run;&lt;BR /&gt;
title;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
 &lt;BR /&gt;
If you have more complex conditions, then you'll have to code for those as well. This was just a simple test. But it should give you an idea of how interleaving works. Remember that the BY in interleaving performs differently than the BY in merging.&lt;BR /&gt;
&lt;BR /&gt;
To learn more about the interleaving process, I recommend this site, &lt;BR /&gt;
&lt;A href="http://support.sas.com/documentation/cdl/en/basess/58133/HTML/default/a001318366.htm" target="_blank"&gt;http://support.sas.com/documentation/cdl/en/basess/58133/HTML/default/a001318366.htm&lt;/A&gt;&lt;BR /&gt;
which has a section entitled: Understanding the Interleaving Process.&lt;BR /&gt;
  &lt;BR /&gt;
cynthia</description>
      <pubDate>Mon, 06 Oct 2008 21:36:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/combining-two-data-sets-conditionally/m-p/52236#M11013</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2008-10-06T21:36:12Z</dc:date>
    </item>
    <item>
      <title>Re: combining two data sets conditionally</title>
      <link>https://communities.sas.com/t5/SAS-Programming/combining-two-data-sets-conditionally/m-p/52237#M11014</link>
      <description>Thanks for the help Cynthia.  Yes, I want only some obs from the second dataset, but the obs that I want do not have a consistent value for the variable I am selecting on.  I only want the obs from the second dataset where variablex (famtrac) in the second dataset matches any value of variablex (famtrac) in the first dataset...&lt;BR /&gt;
&lt;BR /&gt;
In case anyone is also trying to figure out how to do this, here's what I finally came up with:&lt;BR /&gt;
&lt;BR /&gt;
proc sql;&lt;BR /&gt;
create table sql.fam6508&lt;BR /&gt;
	like sql.cps65plus08a;&lt;BR /&gt;
proc sql;&lt;BR /&gt;
insert into sql.fam6508&lt;BR /&gt;
	select * from sql.cps65plus08a  **the primary data set**&lt;BR /&gt;
	union all&lt;BR /&gt;
	select * from sql.cps65under08  **secondary data set**&lt;BR /&gt;
		where famtrac in &lt;BR /&gt;
		(select famtrac from sql.cps65plus08a);</description>
      <pubDate>Wed, 08 Oct 2008 18:38:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/combining-two-data-sets-conditionally/m-p/52237#M11014</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2008-10-08T18:38:46Z</dc:date>
    </item>
    <item>
      <title>Re: combining two data sets conditionally</title>
      <link>https://communities.sas.com/t5/SAS-Programming/combining-two-data-sets-conditionally/m-p/52238#M11015</link>
      <description>An (untested) alternative is the following, assuming the input data sets are sorted on famtrac:&lt;BR /&gt;
&lt;BR /&gt;
data all65plus08 (drop=v1);&lt;BR /&gt;
  set selena.cps65plus08a (in=eldfam) selena.cps65under08 (in=b);&lt;BR /&gt;
  by famtrac;&lt;BR /&gt;
  retain v1;&lt;BR /&gt;
  if eldfam then v1=famtrac;&lt;BR /&gt;
  if eldfam or (b and v1=famtrac);&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
which should give the same result, except for the sort order in the result data set.</description>
      <pubDate>Tue, 18 Nov 2008 22:07:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/combining-two-data-sets-conditionally/m-p/52238#M11015</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2008-11-18T22:07:51Z</dc:date>
    </item>
  </channel>
</rss>

