<?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: KEEP without enumerating dozens of variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/KEEP-without-enumerating-dozens-of-variables/m-p/321773#M71114</link>
    <description>&lt;P&gt;Proc append will append a data set and only have common variables in the result though you need to use FORCE option. So you would only need to Keep the values for dataset_1&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Proc append base=dataset_1 (keep= list to keep) data=dataset_2 Force;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;HOWEVER as with a data step variables of the same name will have to have the same type and if character variables in dataset_2 are longer than in dataset_1 they may be truncated.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If the variables you want from dataset_1 have "nice" names like ABC1, ABC2&amp;nbsp; (a common prefix or start of the variable name) you can use : notation for a list, keep = ABC:&amp;nbsp; would keep all variables that start with the letters ABC, or you could use a range list: Keep = ABC1-ABC9 would keep all the ABC variables with suffixes between 1 and 9. Or the -- (two dashes) gets "sequential" varibles&lt;/P&gt;
&lt;P&gt;Keep a--x&amp;nbsp;&amp;nbsp; would&amp;nbsp;keep the variables a to x in order (if a was the 10th variable column and x was the 15th variable column you would get 10th through 15th variables. This could be modified to request either the numeric or character variables in those columns: a-numeric-x or a-character-x&lt;/P&gt;
&lt;P&gt;or you may be able to use _numeric_ or _character_ if you some of what you want is ALL numeric or ALL character variables.&lt;/P&gt;</description>
    <pubDate>Fri, 30 Dec 2016 18:26:53 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2016-12-30T18:26:53Z</dc:date>
    <item>
      <title>KEEP without enumerating dozens of variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/KEEP-without-enumerating-dozens-of-variables/m-p/321747#M71106</link>
      <description>&lt;P&gt;Hello.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;A dataset_1 has 100 variables&lt;/P&gt;&lt;P&gt;A dataset_2 has 1500&amp;nbsp;variables &amp;nbsp; (50 are common to&amp;nbsp;dataset_1).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to append the observations of dataset_2 to dataset_1, but choosing only the 50&amp;nbsp;common variables, without writing 50 KEEP's, one by one.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is this possible using some kind of KEEP?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;DATA dataset_1;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;KEEP "50common_variables)&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;SET dataset_2;&lt;/P&gt;&lt;P&gt;RUN;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you!&lt;/P&gt;</description>
      <pubDate>Fri, 30 Dec 2016 15:21:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/KEEP-without-enumerating-dozens-of-variables/m-p/321747#M71106</guid>
      <dc:creator>rmlmrmlm</dc:creator>
      <dc:date>2016-12-30T15:21:44Z</dc:date>
    </item>
    <item>
      <title>Re: KEEP without enumerating dozens of variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/KEEP-without-enumerating-dozens-of-variables/m-p/321752#M71108</link>
      <description>&lt;P&gt;Just to clarify ...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Should the final result include only the 50 common variables, or should it include all 100 variables from dataset 1?&lt;/P&gt;</description>
      <pubDate>Fri, 30 Dec 2016 16:24:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/KEEP-without-enumerating-dozens-of-variables/m-p/321752#M71108</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-12-30T16:24:19Z</dc:date>
    </item>
    <item>
      <title>Re: KEEP without enumerating dozens of variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/KEEP-without-enumerating-dozens-of-variables/m-p/321766#M71113</link>
      <description>&lt;P&gt;This is a&amp;nbsp;case where one of the special features of PROC SQL, namely the ability to access metadata via the DICTIONARY container name, is very handy:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
   select a.name into :keeplist separated by ' '
   from
     (select name from dictionary.columns where libname="WORK" and memname="DATASET_1") as a
   inner join
     (select name from dictionary.columns where libname="WORK" and memname="DATASET_2") as b
on a.name=b.name;
quit;

%put &amp;amp;=keeplist;
data want; 
  set d1 (keep=&amp;amp;keeplist)
      d2 (keep=&amp;amp;keeplist) ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There are a LOT of dictionaries available via PROC SQL.&amp;nbsp;&amp;nbsp; Dictionary COLUMNS is the one suitable for&amp;nbsp;your task.&amp;nbsp;Do a "select * from dictionary.dictionaries" to see them all.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now, if your answer to &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4954"&gt;@Astounding&lt;/a&gt;'s question is that you don't want the common variables, but rather all the variables from one of the data sets&amp;nbsp; (i.e. exclude vars only in the 2nd dataset), you don't even need metadata access.&amp;nbsp; Instead, you can just manipulate the program-data-vector constructed by the sas compiler in a data step:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop=_pre _post);
  retain _pre .;
  if 0 then set dataset_1;
  retain _post .;
  set dataset_1 dataset_2;
  keep _pre -- _post ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;Edit entered to initialize the variables (to missing values) in the two RETAIN statements.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;This all depends on how the pdv (program data vector) is enlarged statement-by-statement &lt;STRIKE&gt;step-by-step&lt;/STRIKE&gt; by the sas compiler:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;The leading "retain _pre" statement makes the variable _PRE the leftmost in the PDV.&amp;nbsp; It's the first var seen by the compiler.&lt;/LI&gt;
&lt;LI&gt;The "if 0 then set" statement reads no data, but tells the compiler to make provision for all the variables in dataset_1, just to the right of _PRE.&lt;/LI&gt;
&lt;LI&gt;The "retain _post" put the corresponding variable in the next position, i.e. to the right of the last variable in dataset_1.&lt;/LI&gt;
&lt;LI&gt;the "SET dataset_1 dataset_2"&amp;nbsp; statement not only reads in the data, but forces the compiler to include all the as-yet-unreferenced variables - namely those in dataset_2 that are not in dataset_1.&amp;nbsp; They will all be to the right of _POST.&lt;/LI&gt;
&lt;LI&gt;The keep statement uses the double dash convention to specify a list of vars starting with _PRE and ending with _POST.&lt;/LI&gt;
&lt;LI&gt;Final cleanup: the "drop=" parameter tells the compiler to not allow those vars to go to dataset want.&lt;/LI&gt;
&lt;/OL&gt;</description>
      <pubDate>Fri, 13 Jan 2017 22:57:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/KEEP-without-enumerating-dozens-of-variables/m-p/321766#M71113</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2017-01-13T22:57:04Z</dc:date>
    </item>
    <item>
      <title>Re: KEEP without enumerating dozens of variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/KEEP-without-enumerating-dozens-of-variables/m-p/321773#M71114</link>
      <description>&lt;P&gt;Proc append will append a data set and only have common variables in the result though you need to use FORCE option. So you would only need to Keep the values for dataset_1&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Proc append base=dataset_1 (keep= list to keep) data=dataset_2 Force;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;HOWEVER as with a data step variables of the same name will have to have the same type and if character variables in dataset_2 are longer than in dataset_1 they may be truncated.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If the variables you want from dataset_1 have "nice" names like ABC1, ABC2&amp;nbsp; (a common prefix or start of the variable name) you can use : notation for a list, keep = ABC:&amp;nbsp; would keep all variables that start with the letters ABC, or you could use a range list: Keep = ABC1-ABC9 would keep all the ABC variables with suffixes between 1 and 9. Or the -- (two dashes) gets "sequential" varibles&lt;/P&gt;
&lt;P&gt;Keep a--x&amp;nbsp;&amp;nbsp; would&amp;nbsp;keep the variables a to x in order (if a was the 10th variable column and x was the 15th variable column you would get 10th through 15th variables. This could be modified to request either the numeric or character variables in those columns: a-numeric-x or a-character-x&lt;/P&gt;
&lt;P&gt;or you may be able to use _numeric_ or _character_ if you some of what you want is ALL numeric or ALL character variables.&lt;/P&gt;</description>
      <pubDate>Fri, 30 Dec 2016 18:26:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/KEEP-without-enumerating-dozens-of-variables/m-p/321773#M71114</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2016-12-30T18:26:53Z</dc:date>
    </item>
    <item>
      <title>Re: KEEP without enumerating dozens of variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/KEEP-without-enumerating-dozens-of-variables/m-p/321819#M71126</link>
      <description>&lt;PRE&gt;
It is easy for SQL.



data class1;
 set sashelp.class;
 keep name sex age;
run;
data class2;
 set sashelp.class;
 keep sex age weight;
run;
proc sql;
create table want as
select * from class1
union corresponding all
select * from class2;
quit;
&lt;/PRE&gt;</description>
      <pubDate>Sat, 31 Dec 2016 03:35:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/KEEP-without-enumerating-dozens-of-variables/m-p/321819#M71126</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-12-31T03:35:07Z</dc:date>
    </item>
  </channel>
</rss>

