<?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 Merging long and wide datasets in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Merging-long-and-wide-datasets/m-p/176008#M33750</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I'm trying to merge three long (meaning the each ID has multiple rows of data) datasets but I know that about 150 of the IDs scattered throughout these datasets are people I want to exclude from my analysis. I have another dataset that contains only the IDs I want. How can I create a final long dataset that has all three of the long datasets, but that includes only the IDs that I want? I have tried to use in=a in the datastep but it didn't work. I tried something like this:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data merged;&lt;/P&gt;&lt;P&gt;set long1 long2 long3 idfile (in=a) ;&lt;/P&gt;&lt;P&gt;if a;&lt;/P&gt;&lt;P&gt;by id;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;&lt;P&gt;Laurie&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Tue, 17 Dec 2013 22:49:00 GMT</pubDate>
    <dc:creator>Lefty</dc:creator>
    <dc:date>2013-12-17T22:49:00Z</dc:date>
    <item>
      <title>Merging long and wide datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-long-and-wide-datasets/m-p/176008#M33750</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I'm trying to merge three long (meaning the each ID has multiple rows of data) datasets but I know that about 150 of the IDs scattered throughout these datasets are people I want to exclude from my analysis. I have another dataset that contains only the IDs I want. How can I create a final long dataset that has all three of the long datasets, but that includes only the IDs that I want? I have tried to use in=a in the datastep but it didn't work. I tried something like this:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data merged;&lt;/P&gt;&lt;P&gt;set long1 long2 long3 idfile (in=a) ;&lt;/P&gt;&lt;P&gt;if a;&lt;/P&gt;&lt;P&gt;by id;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;&lt;P&gt;Laurie&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 17 Dec 2013 22:49:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-long-and-wide-datasets/m-p/176008#M33750</guid>
      <dc:creator>Lefty</dc:creator>
      <dc:date>2013-12-17T22:49:00Z</dc:date>
    </item>
    <item>
      <title>Re: Merging long and wide datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-long-and-wide-datasets/m-p/176009#M33751</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;One way might be something like the following untested code:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;data _long1;&lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;&amp;nbsp; merge long1(in=a) idfile (in=b) ;&lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;&amp;nbsp; if a and b;&lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;&amp;nbsp; by id;&lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;run;&lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;&lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;data _long2;&lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;&amp;nbsp; merge long2(in=a) idfile (in=b) ;&lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;&amp;nbsp; if a and b;&lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;&amp;nbsp; by id;&lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;run;&lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;&lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;data _long3;&lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;&amp;nbsp; merge long3(in=a) idfile (in=b) ;&lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;&amp;nbsp; if a and b;&lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;&amp;nbsp; by id;&lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;run;&lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;&lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;data merged;&lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;&amp;nbsp; set _long1 _long2 _long3;&lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;&lt;SPAN style="font-size: 10pt; line-height: 1.5em;"&gt;&amp;nbsp; by id;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;run;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 17 Dec 2013 23:03:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-long-and-wide-datasets/m-p/176009#M33751</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2013-12-17T23:03:08Z</dc:date>
    </item>
    <item>
      <title>Re: Merging long and wide datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-long-and-wide-datasets/m-p/176010#M33752</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Put the IDFILE first in the list and remember the setting of the IN= variable from the first observation for the current ID value.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;data merged;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp; set idfile(in=a) long1 long2 long3 ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp; by id;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp; retain in_idfile;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp; if first.id then in_idfile=a;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp; if in_idfile;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;run;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;If you do not want to include extra records caused by the IDFILE then you might want to change your subsetting IF condition.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;data merged;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp; set idfile(in=a) long1 long2 long3 ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp; by id;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp; retain in_idfile;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp; if first.id then in_idfile=a;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp; if in_idfile and NOT a ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;run;&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 17 Dec 2013 23:32:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-long-and-wide-datasets/m-p/176010#M33752</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2013-12-17T23:32:48Z</dc:date>
    </item>
    <item>
      <title>Re: Merging long and wide datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-long-and-wide-datasets/m-p/176011#M33753</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks so much for your thoughts. I tried it, and it didn't work, but it's because I wasn't very clear in my question. For the 3 long datasets, I don't want to merge them by the ID, I want them to go on top of one another (concatenate?) so each ID still has many rows of data. So it works to combine them by using the set command:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data mergedlong;&lt;/P&gt;&lt;P&gt;set long1 long2 long3;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Then I used (essentially) your code to do the merge of combined long datasets plus the ID file which kicked out the IDs I didn't want:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;proc sort data=mergedlong; by id;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data merged;&lt;/P&gt;&lt;P&gt;merge mergedlong (in=a) idfile (in=b);&lt;/P&gt;&lt;P&gt;if a and b;&lt;/P&gt;&lt;P&gt;by id; run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Worked great! Thanks!!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 17 Dec 2013 23:37:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-long-and-wide-datasets/m-p/176011#M33753</guid>
      <dc:creator>Lefty</dc:creator>
      <dc:date>2013-12-17T23:37:56Z</dc:date>
    </item>
    <item>
      <title>Re: Merging long and wide datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-long-and-wide-datasets/m-p/176012#M33754</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Glad to hear you solved your problem.&amp;nbsp; However, for others facing a similar situation, both Tom's and my suggested code were designed to concatenate, not merge, the 3 files.&amp;nbsp; I really liked Tom's suggested method.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;My guess, as to why it didn't initially work, was that the files weren't all first sorted by id.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;However, the method you selected definitely accomplishes the task, too, and only required the one sort (I presume that the idfile was already sorted).&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 17 Dec 2013 23:50:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-long-and-wide-datasets/m-p/176012#M33754</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2013-12-17T23:50:57Z</dc:date>
    </item>
  </channel>
</rss>

