<?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: merge datasets in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/merge-datasets/m-p/35236#M8670</link>
    <description>The short answer is yes.  However, the "word picture" that you have provided is ambiguous.  Please provide some sample data for input and the output that you want.&lt;BR /&gt;
&lt;BR /&gt;
Doc Muhlbaier&lt;BR /&gt;
Duke</description>
    <pubDate>Mon, 14 Jun 2010 14:00:58 GMT</pubDate>
    <dc:creator>Doc_Duke</dc:creator>
    <dc:date>2010-06-14T14:00:58Z</dc:date>
    <item>
      <title>merge datasets</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/merge-datasets/m-p/35235#M8669</link>
      <description>Hi guys,&lt;BR /&gt;
&lt;BR /&gt;
I am pretty much new in programming in SAS.  Here is my question...&lt;BR /&gt;
&lt;BR /&gt;
Is it possible to merge 2 datasets and output only a certain amount of records if the "by" variables are in dataset A and in dataset B?&lt;BR /&gt;
&lt;BR /&gt;
Example:&lt;BR /&gt;
&lt;BR /&gt;
In dataset A, I have "UserID", "City" , "Prov" and "Date_Registration".&lt;BR /&gt;
In dataset B, I have "City", "Prov" and "NUNITS"&lt;BR /&gt;
&lt;BR /&gt;
I want to output   "UserID", "City" and "Prov" from dataset A, but only the number of records last registered requested (NUNITS) in dataset B.&lt;BR /&gt;
&lt;BR /&gt;
So if I sort dataset A by descending "Date_Registration", I want to output the number of records listed at the top.&lt;BR /&gt;
&lt;BR /&gt;
Thanks a lot for your help!!</description>
      <pubDate>Mon, 14 Jun 2010 13:45:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/merge-datasets/m-p/35235#M8669</guid>
      <dc:creator>chalmyl</dc:creator>
      <dc:date>2010-06-14T13:45:14Z</dc:date>
    </item>
    <item>
      <title>Re: merge datasets</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/merge-datasets/m-p/35236#M8670</link>
      <description>The short answer is yes.  However, the "word picture" that you have provided is ambiguous.  Please provide some sample data for input and the output that you want.&lt;BR /&gt;
&lt;BR /&gt;
Doc Muhlbaier&lt;BR /&gt;
Duke</description>
      <pubDate>Mon, 14 Jun 2010 14:00:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/merge-datasets/m-p/35236#M8670</guid>
      <dc:creator>Doc_Duke</dc:creator>
      <dc:date>2010-06-14T14:00:58Z</dc:date>
    </item>
    <item>
      <title>Re: merge datasets</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/merge-datasets/m-p/35237#M8671</link>
      <description>chalmyl,&lt;BR /&gt;
&lt;BR /&gt;
1) create flag based on last registered date on dataset B. &lt;BR /&gt;
2) When you merge the datasets, output records that meets step 1).&lt;BR /&gt;
&lt;BR /&gt;
GL.</description>
      <pubDate>Mon, 14 Jun 2010 14:53:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/merge-datasets/m-p/35237#M8671</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2010-06-14T14:53:47Z</dc:date>
    </item>
    <item>
      <title>Re: merge datasets</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/merge-datasets/m-p/35238#M8672</link>
      <description>I haven't found a way to attached some data, but here is what I am looking for...&lt;BR /&gt;
&lt;BR /&gt;
Dataset A:&lt;BR /&gt;
&lt;BR /&gt;
USERID        CITY            PROVINCE         DATE_REGISTRATION&lt;BR /&gt;
lyncgar          Ottawa        Ont                        sept12, 2008&lt;BR /&gt;
chalmyl         Ottawa        Ont                        aug23, 2008&lt;BR /&gt;
charman       Ottawa        Ont                        july11, 2008&lt;BR /&gt;
camejef        Ottawa         Qc                         oct01, 2008&lt;BR /&gt;
falardn          Ottawa         Qc                         july14, 2008&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Dataset B;&lt;BR /&gt;
&lt;BR /&gt;
CITY          PROVINCE          NUNITS&lt;BR /&gt;
Ottawa      Ont                          2&lt;BR /&gt;
Ottawa      Qc                           1&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Output:&lt;BR /&gt;
&lt;BR /&gt;
USERID        CITY            PROVINCE         DATE_REGISTRATION&lt;BR /&gt;
lyncgar          Ottawa        Ont                        sept12, 2008&lt;BR /&gt;
chalmyl         Ottawa        Ont                        aug23, 2008&lt;BR /&gt;
camejef        Ottawa         Qc                         oct01, 2008&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
             &lt;BR /&gt;
I tried the following but it only output the 2 first records...&lt;BR /&gt;
&lt;BR /&gt;
proc sort data = A;&lt;BR /&gt;
   by CITY PROVINCE descending DATE_REGISTRATION; &lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
proc sort data = B;&lt;BR /&gt;
   by CITY PROVINCE;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
data test;&lt;BR /&gt;
   merge A B;&lt;BR /&gt;
   by CITY PROVINCE;&lt;BR /&gt;
   retain initial_NUNITS;&lt;BR /&gt;
&lt;BR /&gt;
   if first.CITY then do;&lt;BR /&gt;
     if first.PROVINCE then do;&lt;BR /&gt;
        initial_NUNITS = NUNITS;&lt;BR /&gt;
          i = 0;&lt;BR /&gt;
     end;&lt;BR /&gt;
   end;&lt;BR /&gt;
&lt;BR /&gt;
   if A and B then do;&lt;BR /&gt;
      i + 1;&lt;BR /&gt;
      if i &amp;lt;= initial_NUNITS then output;&lt;BR /&gt;
   end;&lt;BR /&gt;
&lt;BR /&gt;
run;</description>
      <pubDate>Mon, 14 Jun 2010 16:57:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/merge-datasets/m-p/35238#M8672</guid>
      <dc:creator>chalmyl</dc:creator>
      <dc:date>2010-06-14T16:57:37Z</dc:date>
    </item>
    <item>
      <title>Re: merge datasets</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/merge-datasets/m-p/35239#M8673</link>
      <description>I guess my inputs / output are not clear enough.  Sorry...  English is not my primary language...&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Dataset A:&lt;BR /&gt;
&lt;BR /&gt;
USERID    CITY                PROVINCE         DATE_REGISTRATION&lt;BR /&gt;
lyncgar      Ottawa            Ont                        sept12, 2008&lt;BR /&gt;
chalmyl     Ottawa            Ont                        aug23, 2008&lt;BR /&gt;
charman   Ottawa            Ont                        july11, 2008&lt;BR /&gt;
camejef    Ottawa             Qc                         oct01, 2008&lt;BR /&gt;
falardn      Ottawa             Qc                         july14, 2008&lt;BR /&gt;
renadia    Gatineau          Qc                        sept25,2008&lt;BR /&gt;
lavonat     Gatineau         Qc                         jan12,2008&lt;BR /&gt;
philisa     Gatineau          Qc                         jan08,2008&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Dataset B;&lt;BR /&gt;
&lt;BR /&gt;
CITY           PROVINCE        NUNITS&lt;BR /&gt;
Ottawa        Ont                      2&lt;BR /&gt;
Ottawa        Qc                       1&lt;BR /&gt;
Gatineau     Qc                       2&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Output:&lt;BR /&gt;
&lt;BR /&gt;
USERID    CITY                PROVINCE         DATE_REGISTRATION&lt;BR /&gt;
lyncgar      Ottawa            Ont                        sept12, 2008&lt;BR /&gt;
chalmyl     Ottawa            Ont                        aug23, 2008&lt;BR /&gt;
camejef    Ottawa             Qc                         oct01, 2008&lt;BR /&gt;
renadia    Gatineau          Qc                        sept25,2008&lt;BR /&gt;
lavonat     Gatineau         Qc                         jan12,2008</description>
      <pubDate>Mon, 14 Jun 2010 18:12:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/merge-datasets/m-p/35239#M8673</guid>
      <dc:creator>chalmyl</dc:creator>
      <dc:date>2010-06-14T18:12:19Z</dc:date>
    </item>
    <item>
      <title>Re: merge datasets</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/merge-datasets/m-p/35240#M8674</link>
      <description>try this. added data step to keep the latest registration before merge.&lt;BR /&gt;
&lt;BR /&gt;
proc sort data = A;&lt;BR /&gt;
by CITY PROVINCE descending DATE_REGISTRATION; &lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
data aa;&lt;BR /&gt;
set a;               &lt;BR /&gt;
by CITY PROVINCE descending DATE_REGISTRATION; &lt;BR /&gt;
if first.province;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
proc sort data = B;&lt;BR /&gt;
by CITY PROVINCE;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
data test;&lt;BR /&gt;
merge AA B;&lt;BR /&gt;
by CITY PROVINCE;  &lt;BR /&gt;
if A and B ; &lt;BR /&gt;
.&lt;BR /&gt;
.

Message was edited by: sastech</description>
      <pubDate>Mon, 14 Jun 2010 18:17:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/merge-datasets/m-p/35240#M8674</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2010-06-14T18:17:42Z</dc:date>
    </item>
    <item>
      <title>Re: merge datasets</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/merge-datasets/m-p/35241#M8675</link>
      <description>Hi:&lt;BR /&gt;
  I think you are -almost- on the right track. But, you should not need to make dataset AA... the merge can take care of getting you the observations you want. However, the construct &lt;BR /&gt;
[pre]&lt;BR /&gt;
if A and B ; &lt;BR /&gt;
[/pre]&lt;BR /&gt;
  &lt;BR /&gt;
in your MERGE step will not work as you expect unless you also use the IN= data set option in the MERGE statement. Something like the program shown below. Also remember, that when you sort by CITY and PROVINCE that your city of Gatineau will sort BEFORE your city of Ottawa.&lt;BR /&gt;
&lt;BR /&gt;
cynthia&lt;BR /&gt;
  &lt;BR /&gt;
[pre]&lt;BR /&gt;
** read in some test data for A;&lt;BR /&gt;
data A;&lt;BR /&gt;
  infile datalines dlm=' ' dsd;&lt;BR /&gt;
  input userid $ city $ province $ date_registration : anydtdte.;&lt;BR /&gt;
  format date_registration mmddyy10.;&lt;BR /&gt;
return;&lt;BR /&gt;
datalines;&lt;BR /&gt;
lyncgar Ottawa Ont "sept 12, 2008"&lt;BR /&gt;
chalmyl Ottawa Ont "aug 23, 2008"&lt;BR /&gt;
charman Ottawa Ont "july 11, 2008"&lt;BR /&gt;
camejef Ottawa Qc "oct 01, 2008"&lt;BR /&gt;
falardn Ottawa Qc "july 14, 2008"&lt;BR /&gt;
renadia Gatineau Qc "sept 25,2008"&lt;BR /&gt;
lavonat Gatineau Qc "jan 12,2008"&lt;BR /&gt;
philisa Gatineau Qc "jan 08,2008"&lt;BR /&gt;
;&lt;BR /&gt;
run;&lt;BR /&gt;
   &lt;BR /&gt;
** read in some test data for B; &lt;BR /&gt;
Data B;&lt;BR /&gt;
  infile datalines;&lt;BR /&gt;
  input city $ province $ nunits;&lt;BR /&gt;
return;&lt;BR /&gt;
datalines;&lt;BR /&gt;
Ottawa Ont 2&lt;BR /&gt;
Ottawa Qc 1&lt;BR /&gt;
Gatineau Qc 2&lt;BR /&gt;
;&lt;BR /&gt;
run;&lt;BR /&gt;
                     &lt;BR /&gt;
proc sort data=b;&lt;BR /&gt;
  by city province;&lt;BR /&gt;
run;&lt;BR /&gt;
                &lt;BR /&gt;
proc sort data=a;&lt;BR /&gt;
  by city province descending date_registration;&lt;BR /&gt;
run;&lt;BR /&gt;
            &lt;BR /&gt;
ods listing;&lt;BR /&gt;
proc print data=a;&lt;BR /&gt;
  title 'what is in dataset A before merge';&lt;BR /&gt;
run;&lt;BR /&gt;
                 &lt;BR /&gt;
proc print data=b;&lt;BR /&gt;
  title 'what is in dataset B before merge';&lt;BR /&gt;
run;&lt;BR /&gt;
         &lt;BR /&gt;
** The goal in the merge is to use an internal CNTR variable. If there is a match;&lt;BR /&gt;
** between dataset A and dataset B, based on CITY and PROVINCE, then increment the CNTR variable.;&lt;BR /&gt;
** As long as the CNTR variable is less than or equal to the NUNITS variable, then;&lt;BR /&gt;
** output an observation. This also requires that the CNTR variable will get reset to 0 for every;&lt;BR /&gt;
** new FIRST.PROVINCE observation.; &lt;BR /&gt;
** As long as the A dataset is sorted by CITY, PROVINCE and descending DATA_REGISTRATION, then;&lt;BR /&gt;
** the most recent observations will be output, based on the value of NUNITS.;&lt;BR /&gt;
data cntrOK cntr_notOK aonly bonly;&lt;BR /&gt;
  merge b(in=fromb)&lt;BR /&gt;
        a(in=froma);&lt;BR /&gt;
  by city province;&lt;BR /&gt;
  retain cntr;&lt;BR /&gt;
  if first.province then cntr = 0;&lt;BR /&gt;
  if fromb and froma then do;&lt;BR /&gt;
     cntr + 1;&lt;BR /&gt;
     if cntr le nunits then output cntrOK;&lt;BR /&gt;
     else if cntr gt nunits then output cntr_notOK;&lt;BR /&gt;
  end;&lt;BR /&gt;
  else if fromb and not froma then output bonly;&lt;BR /&gt;
  else if froma and not fromb then output aonly;&lt;BR /&gt;
run;&lt;BR /&gt;
                      &lt;BR /&gt;
ods listing;&lt;BR /&gt;
proc print data=cntrOK;&lt;BR /&gt;
  title 'cntrOK data -- desired output';&lt;BR /&gt;
run;&lt;BR /&gt;
              &lt;BR /&gt;
proc print data=cntr_notOK;&lt;BR /&gt;
  title 'cntr_notOK data -- observations where internal cntr was gt nunits';&lt;BR /&gt;
run;&lt;BR /&gt;
               &lt;BR /&gt;
** these may be empty dataset;&lt;BR /&gt;
proc print data=aonly;&lt;BR /&gt;
  title 'what is in dataset AONLY';&lt;BR /&gt;
run;&lt;BR /&gt;
                    &lt;BR /&gt;
proc print data=bonly;&lt;BR /&gt;
  title 'what is in dataset BONLY';&lt;BR /&gt;
run;&lt;BR /&gt;
[/pre]</description>
      <pubDate>Mon, 14 Jun 2010 21:07:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/merge-datasets/m-p/35241#M8675</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2010-06-14T21:07:28Z</dc:date>
    </item>
    <item>
      <title>Re: merge datasets</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/merge-datasets/m-p/35242#M8676</link>
      <description>It's working...  Thanks!!</description>
      <pubDate>Tue, 15 Jun 2010 13:37:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/merge-datasets/m-p/35242#M8676</guid>
      <dc:creator>chalmyl</dc:creator>
      <dc:date>2010-06-15T13:37:46Z</dc:date>
    </item>
  </channel>
</rss>

