<?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: Subset variables from a dataset based on pattern matching in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Subset-variables-from-a-dataset-based-on-pattern-matching/m-p/971555#M43427</link>
    <description>&lt;P&gt;If it's like I wrote, you may try the &lt;A href="https://github.com/SASPAC/baseplus" target="_self"&gt;basePlus&lt;/A&gt;' package macro &lt;A href="https://github.com/SASPAC/baseplus/blob/main/baseplus.md#-getvars-macro--" target="_self"&gt;%getVars()&lt;/A&gt;:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
  q1=1;
  abcQ1efg=2;
  _123Q1456=3;
  q11=4;
  Q12=5;
  xQ1=6;
run;

%loadPackage(basePlus)
/*
%helpPackage(baseplus, '%getVars()' )
*/

%put #%getVars(test,pattern=Q1(?!\d))#;

data want;
  set test;
  keep %getVars(test,pattern=Q1(?!\d));
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
    <pubDate>Mon, 28 Jul 2025 11:47:05 GMT</pubDate>
    <dc:creator>yabwon</dc:creator>
    <dc:date>2025-07-28T11:47:05Z</dc:date>
    <item>
      <title>Subset variables from a dataset based on pattern matching</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Subset-variables-from-a-dataset-based-on-pattern-matching/m-p/971553#M43425</link>
      <description>&lt;P&gt;Dear all,&lt;/P&gt;
&lt;P&gt;I'm trying to subset from a dataset all columns (variables) containing a pattern (named "Q1"). I tried the following:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data myout;
set mydata;
    keep Q1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;but it keeps also Q11,Q12, ...&lt;/P&gt;
&lt;P&gt;Can anyone help me please?&lt;/P&gt;</description>
      <pubDate>Mon, 28 Jul 2025 11:15:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Subset-variables-from-a-dataset-based-on-pattern-matching/m-p/971553#M43425</guid>
      <dc:creator>NewUsrStat</dc:creator>
      <dc:date>2025-07-28T11:15:03Z</dc:date>
    </item>
    <item>
      <title>Re: Subset variables from a dataset based on pattern matching</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Subset-variables-from-a-dataset-based-on-pattern-matching/m-p/971554#M43426</link>
      <description>&lt;P&gt;But Q11 and Q12 contain Q1:&amp;nbsp;&lt;STRONG&gt;Q1&lt;/STRONG&gt;1 and&amp;nbsp;&lt;STRONG&gt;Q1&lt;/STRONG&gt;2&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Does your pattern is xxQ1xx wher "x"es mean letters or _?&lt;/P&gt;
&lt;P&gt;So you need "abcQ1efg" but not "123Q1456"?&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>Mon, 28 Jul 2025 11:32:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Subset-variables-from-a-dataset-based-on-pattern-matching/m-p/971554#M43426</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2025-07-28T11:32:25Z</dc:date>
    </item>
    <item>
      <title>Re: Subset variables from a dataset based on pattern matching</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Subset-variables-from-a-dataset-based-on-pattern-matching/m-p/971555#M43427</link>
      <description>&lt;P&gt;If it's like I wrote, you may try the &lt;A href="https://github.com/SASPAC/baseplus" target="_self"&gt;basePlus&lt;/A&gt;' package macro &lt;A href="https://github.com/SASPAC/baseplus/blob/main/baseplus.md#-getvars-macro--" target="_self"&gt;%getVars()&lt;/A&gt;:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
  q1=1;
  abcQ1efg=2;
  _123Q1456=3;
  q11=4;
  Q12=5;
  xQ1=6;
run;

%loadPackage(basePlus)
/*
%helpPackage(baseplus, '%getVars()' )
*/

%put #%getVars(test,pattern=Q1(?!\d))#;

data want;
  set test;
  keep %getVars(test,pattern=Q1(?!\d));
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Mon, 28 Jul 2025 11:47:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Subset-variables-from-a-dataset-based-on-pattern-matching/m-p/971555#M43427</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2025-07-28T11:47:05Z</dc:date>
    </item>
    <item>
      <title>Re: Subset variables from a dataset based on pattern matching</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Subset-variables-from-a-dataset-based-on-pattern-matching/m-p/971556#M43428</link>
      <description>Hi Bart, thank you for your support. All columns (variables) start with Q*. No Q*in the middle or at the end of a name.</description>
      <pubDate>Mon, 28 Jul 2025 12:11:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Subset-variables-from-a-dataset-based-on-pattern-matching/m-p/971556#M43428</guid>
      <dc:creator>NewUsrStat</dc:creator>
      <dc:date>2025-07-28T12:11:10Z</dc:date>
    </item>
    <item>
      <title>Re: Subset variables from a dataset based on pattern matching</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Subset-variables-from-a-dataset-based-on-pattern-matching/m-p/971558#M43429</link>
      <description>&lt;P&gt;You can use dictionary tables and create a macro variable of the list of desired variable names.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
 select name into :varlist separated by ' '
 from dictionary.columns
 where libname='WORK' and memname='MYDATA' /* must be uppercase */
  and upcase(name) like "Q%";
quit;
%put &amp;amp;varlist;

data myout;
set mydata;
    keep &amp;amp;varlist;
run;

proc print data=myout;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 28 Jul 2025 12:18:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Subset-variables-from-a-dataset-based-on-pattern-matching/m-p/971558#M43429</guid>
      <dc:creator>Kathryn_SAS</dc:creator>
      <dc:date>2025-07-28T12:18:17Z</dc:date>
    </item>
    <item>
      <title>Re: Subset variables from a dataset based on pattern matching</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Subset-variables-from-a-dataset-based-on-pattern-matching/m-p/971559#M43430</link>
      <description>&lt;P&gt;The code you posted would only keep one variable.&amp;nbsp; Perhaps you instead used this keep statement?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;keep q1: ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;That will keep any variable whose name starts with the letter Q followed by the digit 1.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can you provide a simple example that demonstrates the issue?&amp;nbsp; And expound on any variations that you need to support.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For your simple example you could use a combination of KEEP and DROP.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
  length q1 q11 q12 q1test x y z 8;
  keep q1: ;
  drop q11: q12: ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;NOTE: The data set WORK.TEST has 1 observations and 2 variables.
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.00 seconds


16
17   data _null_;
18     set test;
19     put (_all_) (=/);
20   run;


q1=.
q1test=.
NOTE: There were 1 observations read from the data set WORK.TEST.
&lt;/PRE&gt;
&lt;P&gt;But I suspect your issue is more complex than that?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 29 Jul 2025 02:59:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Subset-variables-from-a-dataset-based-on-pattern-matching/m-p/971559#M43430</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-07-29T02:59:07Z</dc:date>
    </item>
    <item>
      <title>Re: Subset variables from a dataset based on pattern matching</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Subset-variables-from-a-dataset-based-on-pattern-matching/m-p/971560#M43431</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/134532"&gt;@NewUsrStat&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Hi Bart, thank you for your support. All columns (variables) start with Q*. No Q*in the middle or at the end of a name.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Examples will help make your question clearer.&lt;/P&gt;
&lt;P&gt;If the variables are all defined with simple numeric suffixes perhaps just using an iterative variable list will work?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;keep q1-q9 ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;would keep variables Q1, Q2, .... Q9 but not keep variable Q12 or Q21 for example.&lt;/P&gt;</description>
      <pubDate>Mon, 28 Jul 2025 12:30:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Subset-variables-from-a-dataset-based-on-pattern-matching/m-p/971560#M43431</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-07-28T12:30:06Z</dc:date>
    </item>
    <item>
      <title>Re: Subset variables from a dataset based on pattern matching</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Subset-variables-from-a-dataset-based-on-pattern-matching/m-p/971561#M43432</link>
      <description>Thank you very much for your support but Q1 and for example Q17 are still subsetted together that is what is not needed. I would like to separate Q1 from Q17 and so on.</description>
      <pubDate>Mon, 28 Jul 2025 12:32:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Subset-variables-from-a-dataset-based-on-pattern-matching/m-p/971561#M43432</guid>
      <dc:creator>NewUsrStat</dc:creator>
      <dc:date>2025-07-28T12:32:06Z</dc:date>
    </item>
    <item>
      <title>Re: Subset variables from a dataset based on pattern matching</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Subset-variables-from-a-dataset-based-on-pattern-matching/m-p/971563#M43433</link>
      <description>&lt;P&gt;OK, so if it suppose to be "Q1xyz...", where x is not a digit (to keep "Q1A", but not "Q17") the pattern should be:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
  q1=1;
  abcQ1efg=2;
  _123Q1456=3;
  q11=4;
  Q12=5;
  xQ1=6;
  q1abc=7;
  q1_efg=8;
run;

%loadPackage(basePlus)
/*
%helpPackage(baseplus, '%getVars()' )
*/

%put #%getVars(test,pattern=^Q1(?!\d))#;

data want;
  set test;
  keep %getVars(test,pattern=^Q1(?!\d));
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;It will select only:&amp;nbsp;q1, q1abc, and q1_efg.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Mon, 28 Jul 2025 13:17:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Subset-variables-from-a-dataset-based-on-pattern-matching/m-p/971563#M43433</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2025-07-28T13:17:02Z</dc:date>
    </item>
    <item>
      <title>Re: Subset variables from a dataset based on pattern matching</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Subset-variables-from-a-dataset-based-on-pattern-matching/m-p/971582#M43435</link>
      <description>&lt;P&gt;But Q11&amp;nbsp;&lt;STRONG&gt;does&lt;/STRONG&gt; contain Q1, so what's the issue?&lt;/P&gt;</description>
      <pubDate>Mon, 28 Jul 2025 18:12:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Subset-variables-from-a-dataset-based-on-pattern-matching/m-p/971582#M43435</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2025-07-28T18:12:53Z</dc:date>
    </item>
    <item>
      <title>Re: Subset variables from a dataset based on pattern matching</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Subset-variables-from-a-dataset-based-on-pattern-matching/m-p/971901#M43442</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/134532"&gt;@NewUsrStat&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Dear all,&lt;/P&gt;
&lt;P&gt;I'm trying to subset from a dataset all columns (variables) containing a pattern (named "Q1"). I tried the following:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data myout;
set mydata;
    &lt;FONT color="#FF6600"&gt;&lt;STRONG&gt;keep Q1;&lt;/STRONG&gt;&lt;/FONT&gt;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;but it keeps also Q11,Q12, ...&lt;/P&gt;
&lt;P&gt;Can anyone help me please?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;No, the keep statement selects only Q1. Nothing else.&lt;/P&gt;</description>
      <pubDate>Fri, 01 Aug 2025 05:46:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Subset-variables-from-a-dataset-based-on-pattern-matching/m-p/971901#M43442</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2025-08-01T05:46:36Z</dc:date>
    </item>
  </channel>
</rss>

