<?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: Get specific variables from all data sets in a library in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Get-specific-variables-from-all-data-sets-in-a-library/m-p/585275#M166888</link>
    <description>&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;I wonder if this can be automated using CALL EXECUTE or by other means for all data sets.&lt;/SPAN&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;Yes, it can be automated.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Use SASHELP.VTABLE which as the list of tables in each library similar to dictionary.columns.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Personally I would recommend using PROC APPEND with a KEEP statement instead. This copies the data set and doesn't do it line by line so would be the fastest approach IMO.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc append base=newName data=oldName (keep = &amp;amp;list_var.);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;FYI - if you had used a naming convention that relied on prefixes rather than suffixes this would be an incredibly trivial solution since you could use short cuts.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's a tutorial on doing this with macros. The last step of using CALL EXECUTE shows how to make it data driven.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://github.com/statgeek/SAS-Tutorials/blob/master/Turning%20a%20program%20into%20a%20macro.md" target="_blank"&gt;https://github.com/statgeek/SAS-Tutorials/blob/master/Turning%20a%20program%20into%20a%20macro.md&lt;/A&gt;&lt;/P&gt;</description>
    <pubDate>Fri, 30 Aug 2019 16:28:23 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2019-08-30T16:28:23Z</dc:date>
    <item>
      <title>Get specific variables from all data sets in a library</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Get-specific-variables-from-all-data-sets-in-a-library/m-p/585261#M166880</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am trying to get specified variables from all data sets in a library. The code shown is just for one data set. I wonder if this can be automated using CALL EXECUTE or by other means for all data sets.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
	select name into: list_var separated by ', '
	from dictionary.columns
	where upcase(libname) = 'X' and upcase(memname) = 'Y' and (strip(upcase(name)) like '%DAY' or strip(upcase(name)) like '%MON' or strip(upcase(name)) like '%YR');
quit;

%put &amp;amp;list_var;

proc sql noprint;

	create table t1 as
	select &amp;amp;list_var
		from X.Y
	;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 30 Aug 2019 16:02:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Get-specific-variables-from-all-data-sets-in-a-library/m-p/585261#M166880</guid>
      <dc:creator>SAS_inquisitive</dc:creator>
      <dc:date>2019-08-30T16:02:04Z</dc:date>
    </item>
    <item>
      <title>Re: Get specific variables from all data sets in a library</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Get-specific-variables-from-all-data-sets-in-a-library/m-p/585275#M166888</link>
      <description>&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;I wonder if this can be automated using CALL EXECUTE or by other means for all data sets.&lt;/SPAN&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;Yes, it can be automated.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Use SASHELP.VTABLE which as the list of tables in each library similar to dictionary.columns.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Personally I would recommend using PROC APPEND with a KEEP statement instead. This copies the data set and doesn't do it line by line so would be the fastest approach IMO.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc append base=newName data=oldName (keep = &amp;amp;list_var.);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;FYI - if you had used a naming convention that relied on prefixes rather than suffixes this would be an incredibly trivial solution since you could use short cuts.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's a tutorial on doing this with macros. The last step of using CALL EXECUTE shows how to make it data driven.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://github.com/statgeek/SAS-Tutorials/blob/master/Turning%20a%20program%20into%20a%20macro.md" target="_blank"&gt;https://github.com/statgeek/SAS-Tutorials/blob/master/Turning%20a%20program%20into%20a%20macro.md&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 30 Aug 2019 16:28:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Get-specific-variables-from-all-data-sets-in-a-library/m-p/585275#M166888</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-08-30T16:28:23Z</dc:date>
    </item>
    <item>
      <title>Re: Get specific variables from all data sets in a library</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Get-specific-variables-from-all-data-sets-in-a-library/m-p/585278#M166891</link>
      <description>&lt;P&gt;You will need to make some decisions about what code you want to create.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;First get the data.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
create table varlist as
  select libname,memname,varnum,name
  from dictionary.columns
  where libname=%upcase('X')
   and (upcase(name) like '%DAY' 
     or upcase(name) like '%MON'
     or upcase(name) like '%YR'
       )
  order by libname,memname,varnum
;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then use the data to generate code. For example here is code to make the subsets into WORK library with the same member name as the dataset had in the X library.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename code temp;
data _null_;
  file code;
  set varlist;
  by memname;
  if first.memname then put
    'data work.' memname ';'
  / '  set ' libname +(-1) '.' memname '(keep=' @
  ;
  put name @;
  if last.memname then put 
    ');'
  / 'run;'
  ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then run the code&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%include code / source2;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 30 Aug 2019 16:31:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Get-specific-variables-from-all-data-sets-in-a-library/m-p/585278#M166891</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-08-30T16:31:37Z</dc:date>
    </item>
    <item>
      <title>Re: Get specific variables from all data sets in a library</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Get-specific-variables-from-all-data-sets-in-a-library/m-p/585291#M166897</link>
      <description>Works great. Thanks, &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;.</description>
      <pubDate>Fri, 30 Aug 2019 16:50:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Get-specific-variables-from-all-data-sets-in-a-library/m-p/585291#M166897</guid>
      <dc:creator>SAS_inquisitive</dc:creator>
      <dc:date>2019-08-30T16:50:18Z</dc:date>
    </item>
    <item>
      <title>Re: Get specific variables from all data sets in a library</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Get-specific-variables-from-all-data-sets-in-a-library/m-p/585293#M166898</link>
      <description>&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;. I was trying to create individuals data sets containing only specified variables for respective data sets in a library.</description>
      <pubDate>Fri, 30 Aug 2019 16:55:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Get-specific-variables-from-all-data-sets-in-a-library/m-p/585293#M166898</guid>
      <dc:creator>SAS_inquisitive</dc:creator>
      <dc:date>2019-08-30T16:55:47Z</dc:date>
    </item>
    <item>
      <title>Re: Get specific variables from all data sets in a library</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Get-specific-variables-from-all-data-sets-in-a-library/m-p/585295#M166899</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/64404"&gt;@SAS_inquisitive&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;. I was trying to create individuals data sets containing only specified variables for respective data sets in a library.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;That is what this code does, did you run the code and try it?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then generating that code dynamically from a data step is trivial.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 30 Aug 2019 17:05:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Get-specific-variables-from-all-data-sets-in-a-library/m-p/585295#M166899</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-08-30T17:05:54Z</dc:date>
    </item>
    <item>
      <title>Re: Get specific variables from all data sets in a library</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Get-specific-variables-from-all-data-sets-in-a-library/m-p/585300#M166900</link>
      <description>&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;, I will try that as well. Thanks !</description>
      <pubDate>Fri, 30 Aug 2019 17:34:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Get-specific-variables-from-all-data-sets-in-a-library/m-p/585300#M166900</guid>
      <dc:creator>SAS_inquisitive</dc:creator>
      <dc:date>2019-08-30T17:34:21Z</dc:date>
    </item>
  </channel>
</rss>

