<?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 Keep specific varaibles from a list in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Keep-specific-varaibles-from-a-list/m-p/634866#M188455</link>
    <description>&lt;P&gt;SAS Version 9.4&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hello Everyone, I have approximately 20 data sets and I would like to subset a few variables that could are present in all data sets. I am not sure if the time varying variable have the same root name &amp;nbsp;&lt;/P&gt;
&lt;P&gt;[For example height would be at visit 1 height1 and at visit 2 height2 (or maybe v2height)]&lt;/P&gt;
&lt;P&gt;so I would like a very flexible code that I could write that would pull and output the variables to a separate data set and their corresponding observations .&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;These are very basic examples of the data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Data set 1 variables&lt;/P&gt;
&lt;P&gt;Name&lt;/P&gt;
&lt;P&gt;ID&lt;/P&gt;
&lt;P&gt;EXAMDATE&lt;/P&gt;
&lt;P&gt;HEIGHT1&lt;/P&gt;
&lt;P&gt;WEIGHT1&lt;/P&gt;
&lt;P&gt;BODYFAT1&lt;/P&gt;
&lt;P&gt;ROOM&lt;/P&gt;
&lt;P&gt;SCHOOL&lt;/P&gt;
&lt;P&gt;CITY&lt;/P&gt;
&lt;P&gt;MOMNAME&lt;/P&gt;
&lt;P&gt;DADNAME&lt;/P&gt;
&lt;P&gt;SIBLINGS&lt;/P&gt;
&lt;P&gt;EYECOLOR&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Data set 2 variable&lt;/P&gt;
&lt;P&gt;NAME&lt;/P&gt;
&lt;P&gt;ID&lt;/P&gt;
&lt;P&gt;EXAMDATE&lt;/P&gt;
&lt;P&gt;HEIGHT2&lt;/P&gt;
&lt;P&gt;WEIGHT2&lt;/P&gt;
&lt;P&gt;BODYFAT2&lt;/P&gt;
&lt;P&gt;ROOM&lt;/P&gt;
&lt;P&gt;SCHOOL&lt;/P&gt;
&lt;P&gt;CITY&lt;/P&gt;
&lt;P&gt;MOMNAME&lt;/P&gt;
&lt;P&gt;DADNAME&lt;/P&gt;
&lt;P&gt;SIBLINGS&lt;/P&gt;
&lt;P&gt;HAIRCOLOR&lt;/P&gt;
&lt;P&gt;Y2COLOR_EYES&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I could do an simple code for each data set such as&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data dataset1_new;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; set dataset1;&lt;/P&gt;
&lt;P&gt;Keep Name &amp;nbsp;ID EXAMDATE HEIGHT1 WEIGHT1 BODYFAT1;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;but I wondered if there was a way to ask SAS to keep only the variable names included in a list but automate it so I do not have to change it for each data set.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;something along the lines of&lt;/P&gt;
&lt;P&gt;if in: (“ID”, “EXAMDATE”, “HEIGHT”, “WEIGHT”, “BODYFAT”, “EYECOLOR”, “Y2COLOR_EYES”) then OUTPUT;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 25 Mar 2020 18:50:51 GMT</pubDate>
    <dc:creator>ajb</dc:creator>
    <dc:date>2020-03-25T18:50:51Z</dc:date>
    <item>
      <title>Keep specific varaibles from a list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Keep-specific-varaibles-from-a-list/m-p/634866#M188455</link>
      <description>&lt;P&gt;SAS Version 9.4&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hello Everyone, I have approximately 20 data sets and I would like to subset a few variables that could are present in all data sets. I am not sure if the time varying variable have the same root name &amp;nbsp;&lt;/P&gt;
&lt;P&gt;[For example height would be at visit 1 height1 and at visit 2 height2 (or maybe v2height)]&lt;/P&gt;
&lt;P&gt;so I would like a very flexible code that I could write that would pull and output the variables to a separate data set and their corresponding observations .&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;These are very basic examples of the data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Data set 1 variables&lt;/P&gt;
&lt;P&gt;Name&lt;/P&gt;
&lt;P&gt;ID&lt;/P&gt;
&lt;P&gt;EXAMDATE&lt;/P&gt;
&lt;P&gt;HEIGHT1&lt;/P&gt;
&lt;P&gt;WEIGHT1&lt;/P&gt;
&lt;P&gt;BODYFAT1&lt;/P&gt;
&lt;P&gt;ROOM&lt;/P&gt;
&lt;P&gt;SCHOOL&lt;/P&gt;
&lt;P&gt;CITY&lt;/P&gt;
&lt;P&gt;MOMNAME&lt;/P&gt;
&lt;P&gt;DADNAME&lt;/P&gt;
&lt;P&gt;SIBLINGS&lt;/P&gt;
&lt;P&gt;EYECOLOR&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Data set 2 variable&lt;/P&gt;
&lt;P&gt;NAME&lt;/P&gt;
&lt;P&gt;ID&lt;/P&gt;
&lt;P&gt;EXAMDATE&lt;/P&gt;
&lt;P&gt;HEIGHT2&lt;/P&gt;
&lt;P&gt;WEIGHT2&lt;/P&gt;
&lt;P&gt;BODYFAT2&lt;/P&gt;
&lt;P&gt;ROOM&lt;/P&gt;
&lt;P&gt;SCHOOL&lt;/P&gt;
&lt;P&gt;CITY&lt;/P&gt;
&lt;P&gt;MOMNAME&lt;/P&gt;
&lt;P&gt;DADNAME&lt;/P&gt;
&lt;P&gt;SIBLINGS&lt;/P&gt;
&lt;P&gt;HAIRCOLOR&lt;/P&gt;
&lt;P&gt;Y2COLOR_EYES&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I could do an simple code for each data set such as&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data dataset1_new;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; set dataset1;&lt;/P&gt;
&lt;P&gt;Keep Name &amp;nbsp;ID EXAMDATE HEIGHT1 WEIGHT1 BODYFAT1;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;but I wondered if there was a way to ask SAS to keep only the variable names included in a list but automate it so I do not have to change it for each data set.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;something along the lines of&lt;/P&gt;
&lt;P&gt;if in: (“ID”, “EXAMDATE”, “HEIGHT”, “WEIGHT”, “BODYFAT”, “EYECOLOR”, “Y2COLOR_EYES”) then OUTPUT;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 25 Mar 2020 18:50:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Keep-specific-varaibles-from-a-list/m-p/634866#M188455</guid>
      <dc:creator>ajb</dc:creator>
      <dc:date>2020-03-25T18:50:51Z</dc:date>
    </item>
    <item>
      <title>Re: Keep specific varaibles from a list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Keep-specific-varaibles-from-a-list/m-p/634873#M188456</link>
      <description>&lt;P&gt;Download the macro %ut_varlist described in &lt;BR /&gt;&lt;A href="https://communities.sas.com/t5/SAS-Programming/Macro-function-to-return-a-list-of-variables-in-a-table-in-a/m-p/633536" target="_blank" rel="noopener"&gt;https://communities.sas.com/t5/SAS-Programming/Macro-function-to-return-a-list-of-variables-in-a-table-in-a/m-p/633536&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%let base_vars=&lt;/P&gt;
&lt;P&gt;NAME&amp;nbsp;ID EXAMDATE&amp;nbsp;HEIGHT&amp;nbsp;WEIGHT&amp;nbsp;BODYFAT&amp;nbsp;ROOM&amp;nbsp;SCHOOL&amp;nbsp;CITY&amp;nbsp;MOMNAME&amp;nbsp;DADNAME&amp;nbsp;SIBLINGS&amp;nbsp; HAIRCOLOR&amp;nbsp;COLOR_EYES;&lt;/P&gt;
&lt;P&gt;%let keep_vars1=%ut_varlist(table=test1, contain=&amp;amp;base_vars.);&lt;/P&gt;
&lt;P&gt;%let keep_vars2=%ut_varlist(table=test2, contain=&amp;amp;base_vars.);&lt;/P&gt;
&lt;P&gt;etc&lt;/P&gt;
&lt;P&gt;If it works for you please like the above posting!&lt;/P&gt;</description>
      <pubDate>Wed, 25 Mar 2020 19:21:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Keep-specific-varaibles-from-a-list/m-p/634873#M188456</guid>
      <dc:creator>DavePrinsloo</dc:creator>
      <dc:date>2020-03-25T19:21:54Z</dc:date>
    </item>
    <item>
      <title>Re: Keep specific varaibles from a list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Keep-specific-varaibles-from-a-list/m-p/634880#M188459</link>
      <description>&lt;P&gt;Are you just looking for the DKRICOND and/or DKROCOND options?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let varlist=Name &amp;nbsp;ID EXAMDATE HEIGHT1 WEIGHT1 BODYFAT1 ;
options dkricond=nowarn;
data want1;
  set have1(keep=&amp;amp;varlist);
run;
data&amp;nbsp;want2;
&amp;nbsp;&amp;nbsp;set&amp;nbsp;have2(keep=&amp;amp;varlist);
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 25 Mar 2020 19:51:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Keep-specific-varaibles-from-a-list/m-p/634880#M188459</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-03-25T19:51:03Z</dc:date>
    </item>
    <item>
      <title>Re: Keep specific varaibles from a list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Keep-specific-varaibles-from-a-list/m-p/634884#M188462</link>
      <description>&lt;P&gt;Look at sas help.vcolumns, it contains all variables of all datasets in libraries currently assigned. If you can define a where-statement for each relevant dataset selecting the variables you want, then you are close to a solution. &lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
  select name into :keepList separated by " "
    from sashelp.vcolumn
    where libname = "LIBRARY" and memname = "DATASET" 
      and /* insert filter here */
  ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;BR /&gt;Then use &amp;amp;varlist in a keep-statement.&lt;/P&gt;
&lt;P&gt;EDIT: The code is now in a code-box.&lt;/P&gt;</description>
      <pubDate>Thu, 26 Mar 2020 05:46:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Keep-specific-varaibles-from-a-list/m-p/634884#M188462</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2020-03-26T05:46:55Z</dc:date>
    </item>
    <item>
      <title>Re: Keep specific varaibles from a list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Keep-specific-varaibles-from-a-list/m-p/635003#M188508</link>
      <description>&lt;P&gt;If you have a number of data sets sharing the same base variable name, meaning the names only vary slightly in &lt;EM&gt;suffix&lt;/EM&gt;, you can use a name prefix list.&amp;nbsp; The list is indicated by using the colon (:) character after the variable name.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Keep 
  Name  ID  EXAMDATE
  HEIGHT:   /* all vars whose name starts with height */
  WEIGHT:  /* all vars whose name starts with weight */
  BODYFAT:  /* all vars whose name starts with bodyfat */
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you want some magic system that finds which columns among many tables are&amp;nbsp;&lt;EM&gt;similarly&amp;nbsp;&lt;/EM&gt; named, that is another question, or this one needs more explanation.&lt;/P&gt;</description>
      <pubDate>Thu, 26 Mar 2020 11:14:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Keep-specific-varaibles-from-a-list/m-p/635003#M188508</guid>
      <dc:creator>RichardDeVen</dc:creator>
      <dc:date>2020-03-26T11:14:25Z</dc:date>
    </item>
    <item>
      <title>Re: Keep specific varaibles from a list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Keep-specific-varaibles-from-a-list/m-p/635006#M188511</link>
      <description>&lt;P&gt;The colon syntax does not work because some columns like have a preceding Y2. The magic system is the %ut_varlist macro in my previous reply &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;The parameter CONTAIN can list any number of core values and may need to use alternatives.&lt;/P&gt;
&lt;P&gt;To expand on my initial reply:&lt;/P&gt;
&lt;P&gt;I modified by base_vars to simply include COLOR, as this will select the columns EYE_COLOR, COLOREYE, Y2EYE_COLOR and HAIRCOLOR, whichever exist in the specified table.&amp;nbsp; Maybe&amp;nbsp;NAME would also be good, as it selects any existing columns like DADNAME, MOMNAME, MOM_NAME, DAD_NAME, DADNAME3 etc.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let base_vars=NAME&amp;nbsp;ID EXAMDATE&amp;nbsp;EXAM_DATE HEIGHT&amp;nbsp;WEIGHT&amp;nbsp;BODYFAT&amp;nbsp;ROOM&amp;nbsp;SCHOOL&amp;nbsp;CITY&amp;nbsp;NAME&amp;nbsp;SIBLINGS&amp;nbsp; COLOR&amp;nbsp; ;
%let keep_vars1=%ut_varlist(table=test1, contain=&amp;amp;base_vars.);
%let keep_vars2=%ut_varlist(table=test2, contain=&amp;amp;base_vars.);
etc.&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 26 Mar 2020 11:29:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Keep-specific-varaibles-from-a-list/m-p/635006#M188511</guid>
      <dc:creator>DavePrinsloo</dc:creator>
      <dc:date>2020-03-26T11:29:20Z</dc:date>
    </item>
    <item>
      <title>Re: Keep specific varaibles from a list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Keep-specific-varaibles-from-a-list/m-p/635011#M188516</link>
      <description>&lt;P&gt;Nice options, but I personally am moving in the opposite direction, such as using the NOTE2ERR option, since that forces me&amp;nbsp; to immediately avoid having columns created that are not spelled correctly, etc.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As a consultant , I have recently been migrating some code, and found logs that are riddled with notes about missing values, etc. which could cause problems / create missing values in some cases.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 26 Mar 2020 11:41:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Keep-specific-varaibles-from-a-list/m-p/635011#M188516</guid>
      <dc:creator>DavePrinsloo</dc:creator>
      <dc:date>2020-03-26T11:41:43Z</dc:date>
    </item>
    <item>
      <title>Re: Keep specific varaibles from a list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Keep-specific-varaibles-from-a-list/m-p/635050#M188525</link>
      <description>&lt;P&gt;With all these name variants in different tables, do you ever plan to combine the tables and normalize the names through some mapping ?&lt;/P&gt;</description>
      <pubDate>Thu, 26 Mar 2020 14:36:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Keep-specific-varaibles-from-a-list/m-p/635050#M188525</guid>
      <dc:creator>RichardDeVen</dc:creator>
      <dc:date>2020-03-26T14:36:46Z</dc:date>
    </item>
  </channel>
</rss>

