<?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: select list of variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/select-list-of-variables/m-p/695760#M212367</link>
    <description>&lt;P&gt;Sir &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt;&amp;nbsp;Immortals are so unassuming, while mere mortals like me can only worship "kolmogorov equals" and not become one. End of the Sunday story!&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sun, 01 Nov 2020 17:28:38 GMT</pubDate>
    <dc:creator>novinosrin</dc:creator>
    <dc:date>2020-11-01T17:28:38Z</dc:date>
    <item>
      <title>select list of variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/select-list-of-variables/m-p/695749#M212358</link>
      <description>&lt;P&gt;Hello:&lt;/P&gt;&lt;P&gt;I have a data set that has more than 300 variables. I would like to select some variables that have the same prefix and suffix. e.g I only want to keep variables with prefix "s" and suffix "2020". Please my code for short version of my data set. The code includes something I tried but not working.&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input var1 var2 s12020 t12020 u12020 s22020 t22020 u22020 s32020 t32020 u32020;
cards;
1 2 3 4 5 6 7 8 9 1 2
;
run;
 
/* I would like to keep s12020 s22020 s32020, this is an example, I have longer var list than this in real data */
/*
data want;
set have (keep= s12020-s32020);
run;
ERROR: Not all variables in the list s12020-s32020 were found.
*/&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 01 Nov 2020 15:43:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/select-list-of-variables/m-p/695749#M212358</guid>
      <dc:creator>sasecn</dc:creator>
      <dc:date>2020-11-01T15:43:12Z</dc:date>
    </item>
    <item>
      <title>Re: select list of variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/select-list-of-variables/m-p/695750#M212359</link>
      <description>&lt;P&gt;If I may recommend the BasePlus package (here is the doc: &lt;A href="https://github.com/yabwon/SAS_PACKAGES/blob/master/packages/baseplus.md)" target="_blank"&gt;https://github.com/yabwon/SAS_PACKAGES/blob/master/packages/baseplus.md)&lt;/A&gt;&amp;nbsp;and the %getVars() macro.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename packages "%sysfunc(pathname(work))"; /* setup temporary directory for packages in the WORK */
filename SPFinit url "https://raw.githubusercontent.com/yabwon/SAS_PACKAGES/master/SPF/SPFinit.sas";
%include SPFinit; /* enable the framework */

%installPackage(BasePlus) /* install the package */
%loadPackage(BasePlus)    /* load the package */

data have;
input var1 var2 s12020 t12020 u12020 s22020 t22020 u22020 s32020 t32020 u32020;
cards;
1 2 3 4 5 6 7 8 9 1 2
;
run;

data want;
  set have (keep= %getVars(have, pattern=^s.*2020$, varRange=_numeric_));
  put _ALL_;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 01 Nov 2020 15:54:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/select-list-of-variables/m-p/695750#M212359</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2020-11-01T15:54:33Z</dc:date>
    </item>
    <item>
      <title>Re: select list of variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/select-list-of-variables/m-p/695752#M212360</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/84484"&gt;@sasecn&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hello:&lt;/P&gt;
&lt;P&gt;I have a data set that has more than 300 variables. I would like to select some variables that have the same prefix and suffix. e.g I only want to keep variables with prefix "s" and suffix "2020". &lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;If you have the ability to choose the names in your data set, you really ought to name the variable differently so you can choose the variables much more easily. Make the prefix s2020 instead of prefix s and suffix 2020. Then you can select them all using the colon after s2020, like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have (keep= s2020:);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 01 Nov 2020 16:17:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/select-list-of-variables/m-p/695752#M212360</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-11-01T16:17:01Z</dc:date>
    </item>
    <item>
      <title>Re: select list of variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/select-list-of-variables/m-p/695753#M212361</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/35763"&gt;@yabwon&lt;/a&gt;&amp;nbsp; Sir Bart(the genie). If I knew you few years ago, I would have done an MBA and sought a management or political career of sorts and cleverly outsourced all my work to you. Kudos! Now I see the value of the packages and routines.&amp;nbsp; Bro, that's a lot of effort. Jeez well done!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Anyways, I wanted to have some fun too-&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input var1 var2 s12020 t12020 u12020 s22020 t22020 u22020 s32020 t32020 u32020;
cards;
1 2 3 4 5 6 7 8 9 1 2
8 7 6 5 4 3 2 1 0 9 7
;
run;

data _null_;
 set have end=z;
 if _n_=1 then do;
  dcl hash h(multidata:'y',ordered:'y');
  h.definekey('_n_');
  array t _numeric_;
  do over t;
   if prxmatch('/^s.*2020$/',vname(t)) then h.definedata(vname(t));
  end;
  h.definedone();
 end;
 h.add();
 if z;
 h.output(dataset:'want');
run;


proc print noobs;run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 01 Nov 2020 16:39:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/select-list-of-variables/m-p/695753#M212361</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-11-01T16:39:18Z</dc:date>
    </item>
    <item>
      <title>Re: select list of variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/select-list-of-variables/m-p/695754#M212362</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you for kind words! &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; The biggest joy is when I see that SPF is working "in the battle field"&lt;/P&gt;
&lt;P&gt;I've just had&amp;nbsp;new version released:&amp;nbsp;&lt;A href="https://communities.sas.com/t5/SAS-Programming/SAS-Packages-Framework-version-20201101/m-p/695742" target="_blank"&gt;https://communities.sas.com/t5/SAS-Programming/SAS-Packages-Framework-version-20201101/m-p/695742&lt;/A&gt;&amp;nbsp;You can&amp;nbsp;&lt;span class="lia-unicode-emoji" title=":thumbs_up:"&gt;👍&lt;/span&gt; if you want &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Btw. your use of hash table is also very elegant.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;All the best&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Sun, 01 Nov 2020 16:51:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/select-list-of-variables/m-p/695754#M212362</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2020-11-01T16:51:48Z</dc:date>
    </item>
    <item>
      <title>Re: select list of variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/select-list-of-variables/m-p/695755#M212363</link>
      <description>&lt;P&gt;There is no "&lt;EM&gt;if i want&lt;/EM&gt;" for such "meritorious deeds". Done! My oh my, I can't believe I missed that post. Indeed deserves heaps of praise. Thank you for reminding me. Also, I am gonna take a week long off end of this month, and I would utilize that time to read and understand your SAS packages.&amp;nbsp; Once again, very well done! Great stuff!&lt;/P&gt;</description>
      <pubDate>Sun, 01 Nov 2020 17:01:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/select-list-of-variables/m-p/695755#M212363</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-11-01T17:01:32Z</dc:date>
    </item>
    <item>
      <title>Re: select list of variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/select-list-of-variables/m-p/695756#M212364</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you fancy there will be a BASUG seminar about SPF&amp;nbsp;in November (thanks go to&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/19879"&gt;@Quentin&lt;/a&gt;).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Registration is open:&amp;nbsp;&lt;A href="https://us02web.zoom.us/webinar/register/WN_znATxbA4TvactunkegVFAA" target="_blank"&gt;https://us02web.zoom.us/webinar/register/WN_znATxbA4TvactunkegVFAA&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 01 Nov 2020 17:07:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/select-list-of-variables/m-p/695756#M212364</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2020-11-01T17:07:40Z</dc:date>
    </item>
    <item>
      <title>Re: select list of variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/select-list-of-variables/m-p/695757#M212365</link>
      <description>&lt;P&gt;One more for the fun record-&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  set have;
  length n $32 var_list $32767;
  do while(1);
   call vnext(n);
   if n=' ' then leave;
   if not prxmatch('/^s.*2020$/',strip(n))  then continue;
   var_list=catx(' ',var_list,n);
  end;
  call symputx('var_list',var_list,'g');
  stop;
 run;

%put &amp;amp;=var_list;

data want;
 set have(keep=&amp;amp;var_list);
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 01 Nov 2020 17:20:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/select-list-of-variables/m-p/695757#M212365</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-11-01T17:20:19Z</dc:date>
    </item>
    <item>
      <title>Re: select list of variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/select-list-of-variables/m-p/695758#M212366</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/84484"&gt;@sasecn&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;After all these creative suggestions have been made, I'm hesitant to add a boring PROC SQL approach:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
select name into :sxx2020 separated by ' '
from dictionary.columns
where libname='WORK' &amp;amp; memname='HAVE' &amp;amp; name like 's%2020';
quit;

data want;
set have(keep=&amp;amp;sxx2020);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Edit: Similarly, you can use SASHELP.VCOLUMN and combine the two steps above into one DATA _NULL_ step:&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
set sashelp.vcolumn end=last;
where libname='WORK' &amp;amp; memname='HAVE' &amp;amp; name like 's%2020';
if _n_=1 then call execute('data want; set have(keep=');
call execute(name);
if last then call execute('); run;');
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 01 Nov 2020 17:32:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/select-list-of-variables/m-p/695758#M212366</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2020-11-01T17:32:29Z</dc:date>
    </item>
    <item>
      <title>Re: select list of variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/select-list-of-variables/m-p/695760#M212367</link>
      <description>&lt;P&gt;Sir &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt;&amp;nbsp;Immortals are so unassuming, while mere mortals like me can only worship "kolmogorov equals" and not become one. End of the Sunday story!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 01 Nov 2020 17:28:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/select-list-of-variables/m-p/695760#M212367</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-11-01T17:28:38Z</dc:date>
    </item>
    <item>
      <title>Re: select list of variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/select-list-of-variables/m-p/695981#M212477</link>
      <description>&lt;P&gt;If "12020" in any way relates to January 2020, then you are 1) hiding data values in variable names and 2) likely have a suboptimal data structure and might be better off transposing the data and adding a date variable so you have one record per "month" with names like S T and U.&lt;/P&gt;</description>
      <pubDate>Mon, 02 Nov 2020 16:17:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/select-list-of-variables/m-p/695981#M212477</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-11-02T16:17:49Z</dc:date>
    </item>
  </channel>
</rss>

