<?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 Only One Variable from 41 Datasets in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Merging-Only-One-Variable-from-41-Datasets/m-p/481495#M286764</link>
    <description>&lt;P&gt;I&amp;nbsp;have 41 datasets that have 7 variables in each one. Each dataset is named based on&amp;nbsp;an air traffic control tower and each variable in the dataset is named based on&amp;nbsp;said control tower. So I have 41 datasets named like CYYZ_PERCENT_OAG_F. The only thing that changes is the first four character tower identifier. Another dataset is called CYVR_PERCENT_OAG_F.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As I wrote above, each data set has seven variables in it. They are:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Year&amp;nbsp; Month&amp;nbsp; CYYZ_COUNT_ALL&amp;nbsp; CYYZ_COUNT_OAG&amp;nbsp; CYYZ_COUNT_NON_OAG&amp;nbsp; CYYZ_OAG_PERCENT&amp;nbsp; CYYZ_NON_OAG_PERCENT&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The tower identifier, CYYZ in this case, changes for each variable in each dataset.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like to merge all 41 datasets together based on year and month and&amp;nbsp;keep ONLY the CYYZ_OAG_PERCENT&amp;nbsp;variable. So the&amp;nbsp;new dataset&amp;nbsp;would be like:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Year&amp;nbsp; Month&amp;nbsp; CYYZ_OAG_PERCENT&amp;nbsp; CYVR_OAG_PERCENT&amp;nbsp; CYYC_OAG_PERCENT&amp;nbsp; etc.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is there a way to do this ?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;&lt;P&gt;-Bill&lt;/P&gt;</description>
    <pubDate>Thu, 26 Jul 2018 13:36:04 GMT</pubDate>
    <dc:creator>BCNAV</dc:creator>
    <dc:date>2018-07-26T13:36:04Z</dc:date>
    <item>
      <title>Merging Only One Variable from 41 Datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-Only-One-Variable-from-41-Datasets/m-p/481495#M286764</link>
      <description>&lt;P&gt;I&amp;nbsp;have 41 datasets that have 7 variables in each one. Each dataset is named based on&amp;nbsp;an air traffic control tower and each variable in the dataset is named based on&amp;nbsp;said control tower. So I have 41 datasets named like CYYZ_PERCENT_OAG_F. The only thing that changes is the first four character tower identifier. Another dataset is called CYVR_PERCENT_OAG_F.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As I wrote above, each data set has seven variables in it. They are:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Year&amp;nbsp; Month&amp;nbsp; CYYZ_COUNT_ALL&amp;nbsp; CYYZ_COUNT_OAG&amp;nbsp; CYYZ_COUNT_NON_OAG&amp;nbsp; CYYZ_OAG_PERCENT&amp;nbsp; CYYZ_NON_OAG_PERCENT&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The tower identifier, CYYZ in this case, changes for each variable in each dataset.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like to merge all 41 datasets together based on year and month and&amp;nbsp;keep ONLY the CYYZ_OAG_PERCENT&amp;nbsp;variable. So the&amp;nbsp;new dataset&amp;nbsp;would be like:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Year&amp;nbsp; Month&amp;nbsp; CYYZ_OAG_PERCENT&amp;nbsp; CYVR_OAG_PERCENT&amp;nbsp; CYYC_OAG_PERCENT&amp;nbsp; etc.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is there a way to do this ?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;&lt;P&gt;-Bill&lt;/P&gt;</description>
      <pubDate>Thu, 26 Jul 2018 13:36:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-Only-One-Variable-from-41-Datasets/m-p/481495#M286764</guid>
      <dc:creator>BCNAV</dc:creator>
      <dc:date>2018-07-26T13:36:04Z</dc:date>
    </item>
    <item>
      <title>Re: Merging Only One Variable from 41 Datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-Only-One-Variable-from-41-Datasets/m-p/481503#M286765</link>
      <description>&lt;P&gt;Unfortunately your variable names do not lend themselves to lists or prefixes which could help you here.&amp;nbsp; So you would need to do this manually, into a datastep then keep that.&amp;nbsp; E.g.:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;/* This merges all the data - assumes sorted and in a lib called datadir */&lt;BR /&gt;data want;
  merge datadir.c:;
  by year month;
run;&lt;BR /&gt;/* Get list of percent vars */&lt;BR /&gt;proc sql;&lt;BR /&gt;  select varname&lt;BR /&gt;  into   vlist separated by " "&lt;BR /&gt;  from  dictionary.columns&lt;BR /&gt;  where libname="WORK"&lt;BR /&gt;    and memname="WANT"&lt;BR /&gt;    and index(varname,"PERCENT");&lt;BR /&gt;quit;&lt;BR /&gt;/* Now keep those */&lt;BR /&gt;data want (keep=year month &amp;amp;vlist.);&lt;BR /&gt;  set want;&lt;BR /&gt;run;  &lt;/PRE&gt;
&lt;P&gt;Its much easier coding however if you model your data correctly.&lt;/P&gt;</description>
      <pubDate>Thu, 26 Jul 2018 13:47:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-Only-One-Variable-from-41-Datasets/m-p/481503#M286765</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-07-26T13:47:12Z</dc:date>
    </item>
    <item>
      <title>Re: Merging Only One Variable from 41 Datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-Only-One-Variable-from-41-Datasets/m-p/481509#M286766</link>
      <description>&lt;P&gt;Another classic example why it is a VERY BAD IDEA to have data (tower names) in structure (variable names).&lt;/P&gt;
&lt;P&gt;Now you have to go through some complicated macro or call execute() programming to solve something that should be a non-issue in the first place.&lt;/P&gt;
&lt;P&gt;And then you go and perpetuate that by making the same, SILLY mistake again.&lt;/P&gt;
&lt;P&gt;Your target dataset should be&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;year month tower oag_percent&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you need a wide format for display in the end, you just need a single proc transpose step. But don't saddle yourself with wide datasets for processing, unless you have a very strong masochistic streak.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Suppose you have a list of towers in a dataset:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data towers;
input tower $;
cards;
CYYZ
CYVR
CYYC
;
run;

data _null_;
call execute('data want; set');
do until (eof);
  set towers end=eof;
  call execute(' library.' !! strip(tower ));
end;
call execute(' indsname=inds;');
call execute('tower=substr(scan(inds,2,"."),1,4);select(tower);');
do until (eof);
  set towers end=eof;
  call execute('when("' !! strip(tower) !! '") oag_percent = ' !! strip(tower) !! '_oag_percent;');
end;
call execute('end; keep year month tower oag_percent; run;');
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;will give you a nice longitudinal dataset that's easy to deal with.&lt;/P&gt;</description>
      <pubDate>Thu, 26 Jul 2018 14:02:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-Only-One-Variable-from-41-Datasets/m-p/481509#M286766</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-07-26T14:02:38Z</dc:date>
    </item>
    <item>
      <title>Re: Merging Only One Variable from 41 Datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-Only-One-Variable-from-41-Datasets/m-p/481515#M286767</link>
      <description>&lt;P&gt;The idea is to create a MERGE statement that looks like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;merge CYYZ (keep=year month CYYZ_OAG_PERCENT)&lt;/P&gt;
&lt;P&gt;CYVR (keep=year month CYVR_OAG_PERCENT)&lt;/P&gt;
&lt;P&gt;CYYC (keep=year month CYYC_OAG_PERCENT)&lt;/P&gt;
&lt;P&gt;etc.&lt;/P&gt;
&lt;P&gt;;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To make this easier, less error-prone,&amp;nbsp;and understandable at the same time, I would recommend creating a small macro:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%macro tower (towername);&lt;/P&gt;
&lt;P&gt;&amp;amp;towername (keep=year month &amp;amp;towername._OAG_PERCENT)&lt;/P&gt;
&lt;P&gt;%mend tower;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Having done that, you can construct the MERGE statement by calling the macro 41 times:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;options mprint;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;merge&lt;/P&gt;
&lt;P&gt;%tower (CYYZ)&lt;/P&gt;
&lt;P&gt;%tower (CYVR)&lt;/P&gt;
&lt;P&gt;%tower (CYYC)&lt;/P&gt;
&lt;P&gt;etc.&lt;/P&gt;
&lt;P&gt;;&lt;/P&gt;
&lt;P&gt;by year month;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;</description>
      <pubDate>Thu, 26 Jul 2018 14:17:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-Only-One-Variable-from-41-Datasets/m-p/481515#M286767</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2018-07-26T14:17:24Z</dc:date>
    </item>
  </channel>
</rss>

