<?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: How to keep variables based on suffix in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-keep-variables-based-on-suffix/m-p/645704#M193047</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/194348"&gt;@GS2&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I want to keep the "_co". I want to split the data in 2, so I dropped the "_co" in 1 but want to KEEP in a new data set.&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;So take the macro variable with the list of variable names and use it in two places.&amp;nbsp; You can do the split in one step if you want by using dataset options on the output datsets.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data cases_with(keep=&amp;amp;vars) case_without(drop=&amp;amp;vars);
  set work.have;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 06 May 2020 19:01:29 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2020-05-06T19:01:29Z</dc:date>
    <item>
      <title>How to keep variables based on suffix</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-keep-variables-based-on-suffix/m-p/645650#M193033</link>
      <description>&lt;P&gt;Using SAS 9.4&lt;/P&gt;&lt;P&gt;I have 84 variables in a data set and half of them are labeled "_co". I want to keep only the variables with "_co". I have seen a lot of code for how to drop based on suffix but I have not seen any accepted answers on how to keep. The other half of the variables do not have a suffix or prefix that would make it easy to drop based on that. Any help or guidance would be appreciated. Thank you&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 06 May 2020 16:14:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-keep-variables-based-on-suffix/m-p/645650#M193033</guid>
      <dc:creator>GS2</dc:creator>
      <dc:date>2020-05-06T16:14:55Z</dc:date>
    </item>
    <item>
      <title>Re: How to keep variables based on suffix</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-keep-variables-based-on-suffix/m-p/645651#M193034</link>
      <description>&lt;P&gt;Just use the code you found and change the keyword DROP to the keyword KEEP.&lt;/P&gt;
&lt;P&gt;Try it. If it doesn't work then post an actual example and either show the error or explain how it is not doing what you want.&lt;/P&gt;</description>
      <pubDate>Wed, 06 May 2020 16:16:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-keep-variables-based-on-suffix/m-p/645651#M193034</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-05-06T16:16:51Z</dc:date>
    </item>
    <item>
      <title>Re: How to keep variables based on suffix</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-keep-variables-based-on-suffix/m-p/645652#M193035</link>
      <description>&lt;P&gt;Here is a reference that illustrates how to refer to variables and datasets in a short cut list:&lt;BR /&gt;&lt;A href="https://blogs.sas.com/content/iml/2018/05/29/6-easy-ways-to-specify-a-list-of-variables-in-sas.html" target="_blank"&gt;https://blogs.sas.com/content/iml/2018/05/29/6-easy-ways-to-specify-a-list-of-variables-in-sas.html&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can you use one of the other shortcut methods to list out the variables you want to keep/drop?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/194348"&gt;@GS2&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Using SAS 9.4&lt;/P&gt;
&lt;P&gt;I have 84 variables in a data set and half of them are labeled "_co". I want to keep only the variables with "_co". I have seen a lot of code for how to drop based on suffix but I have not seen any accepted answers on how to keep. The other half of the variables do not have a suffix or prefix that would make it easy to drop based on that. Any help or guidance would be appreciated. Thank you&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 06 May 2020 16:20:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-keep-variables-based-on-suffix/m-p/645652#M193035</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-05-06T16:20:33Z</dc:date>
    </item>
    <item>
      <title>Re: How to keep variables based on suffix</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-keep-variables-based-on-suffix/m-p/645653#M193036</link>
      <description>&lt;P&gt;%let suffix=_co;&lt;/P&gt;&lt;P&gt;proc sql noprint;&lt;BR /&gt;select name into :vars separated by ' '&lt;BR /&gt;from dictionary.columns&lt;BR /&gt;where upcase(libname)=upcase('work')&lt;BR /&gt;and upcase(memname)=upcase('have')&lt;BR /&gt;and upcase(substr(name,length(name)-(length("&amp;amp;suffix")-1),length("&amp;amp;suffix")))=upcase("&amp;amp;suffix");&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;%put &amp;amp;vars;&lt;/P&gt;&lt;P&gt;data cases;&lt;BR /&gt;set work.have;&lt;BR /&gt;if length("&amp;amp;vars") ne 0 then do;&lt;BR /&gt;drop &amp;amp;vars;&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is the code I ran&lt;/P&gt;</description>
      <pubDate>Wed, 06 May 2020 16:21:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-keep-variables-based-on-suffix/m-p/645653#M193036</guid>
      <dc:creator>GS2</dc:creator>
      <dc:date>2020-05-06T16:21:30Z</dc:date>
    </item>
    <item>
      <title>Re: How to keep variables based on suffix</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-keep-variables-based-on-suffix/m-p/645654#M193037</link>
      <description>&lt;P&gt;DROP/KEEP statements are not conditional so that IF/THEN does nothing.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Change DROP to KEEP and it will keep the variables. The SQL just creates the list of variables to keep that you can slot in to the data step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Open the sashelp.vcolumn table to see what's in it and how it's being used.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 06 May 2020 16:24:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-keep-variables-based-on-suffix/m-p/645654#M193037</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-05-06T16:24:15Z</dc:date>
    </item>
    <item>
      <title>Re: How to keep variables based on suffix</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-keep-variables-based-on-suffix/m-p/645656#M193039</link>
      <description>&lt;P&gt;So do you want to find the variables to KEEP or those to DROP?&lt;/P&gt;
&lt;P&gt;You posted code that is using the list of variable in a DROP statement, not a KEEP statement.&lt;/P&gt;</description>
      <pubDate>Wed, 06 May 2020 16:27:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-keep-variables-based-on-suffix/m-p/645656#M193039</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-05-06T16:27:35Z</dc:date>
    </item>
    <item>
      <title>Re: How to keep variables based on suffix</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-keep-variables-based-on-suffix/m-p/645661#M193041</link>
      <description>&lt;P&gt;I want to keep the "_co". I want to split the data in 2, so I dropped the "_co" in 1 but want to KEEP in a new data set.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 06 May 2020 17:23:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-keep-variables-based-on-suffix/m-p/645661#M193041</guid>
      <dc:creator>GS2</dc:creator>
      <dc:date>2020-05-06T17:23:40Z</dc:date>
    </item>
    <item>
      <title>Re: How to keep variables based on suffix</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-keep-variables-based-on-suffix/m-p/645663#M193043</link>
      <description>&lt;P&gt;You may want to look at Rick Wilkin's example of how he has used SAS Jedi's macros to keep variables with a certain suffix. Looks like what you want.&amp;nbsp;&amp;nbsp;&lt;A href="https://blogs.sas.com/content/iml/2019/10/28/regular-expressions-variable-names-sas.html" target="_blank"&gt;https://blogs.sas.com/content/iml/2019/10/28/regular-expressions-variable-names-sas.html&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You will need to download the macros from Jedi's post and they may be useful later for a lot of other tasks.&lt;/P&gt;&lt;P&gt;&lt;A href="https://blogs.sas.com/content/sastraining/2017/08/29/sas-variable-lists-by-pattern/" target="_blank"&gt;https://blogs.sas.com/content/sastraining/2017/08/29/sas-variable-lists-by-pattern/&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 06 May 2020 17:33:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-keep-variables-based-on-suffix/m-p/645663#M193043</guid>
      <dc:creator>biopharma</dc:creator>
      <dc:date>2020-05-06T17:33:47Z</dc:date>
    </item>
    <item>
      <title>Re: How to keep variables based on suffix</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-keep-variables-based-on-suffix/m-p/645702#M193046</link>
      <description>&lt;P&gt;Copy the macro ut_varlist posted in:&lt;/P&gt;
&lt;P&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;to your macro library.&amp;nbsp; Then you can use it to build macro variable lists embedded in code, for example:&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=%ut_varlist(table=have,contain=_co, contain_pos=end));
run;

OR

proc sql noprint;
create table want as
select %ut_varlist(table=have,contain=_co, contain_pos=end), newdlm=%str(,))
from have;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 06 May 2020 18:59:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-keep-variables-based-on-suffix/m-p/645702#M193046</guid>
      <dc:creator>DavePrinsloo</dc:creator>
      <dc:date>2020-05-06T18:59:46Z</dc:date>
    </item>
    <item>
      <title>Re: How to keep variables based on suffix</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-keep-variables-based-on-suffix/m-p/645704#M193047</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/194348"&gt;@GS2&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I want to keep the "_co". I want to split the data in 2, so I dropped the "_co" in 1 but want to KEEP in a new data set.&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;So take the macro variable with the list of variable names and use it in two places.&amp;nbsp; You can do the split in one step if you want by using dataset options on the output datsets.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data cases_with(keep=&amp;amp;vars) case_without(drop=&amp;amp;vars);
  set work.have;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 06 May 2020 19:01:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-keep-variables-based-on-suffix/m-p/645704#M193047</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-05-06T19:01:29Z</dc:date>
    </item>
    <item>
      <title>Re: How to keep variables based on suffix</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-keep-variables-based-on-suffix/m-p/645707#M193048</link>
      <description>&lt;P&gt;Further example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let co_vars=%ut_varlist(table=have, contain=_co, contain_pos=end);
%let other_vars=%ut_varlsit(table=have, exclude=&amp;amp;co_vars.);

data want_co (keep=&amp;amp;co_vars.)
        want_others(keep=&amp;amp;other_vars.);
set have;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 06 May 2020 19:07:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-keep-variables-based-on-suffix/m-p/645707#M193048</guid>
      <dc:creator>DavePrinsloo</dc:creator>
      <dc:date>2020-05-06T19:07:55Z</dc:date>
    </item>
    <item>
      <title>Re: How to keep variables based on suffix</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-keep-variables-based-on-suffix/m-p/645709#M193049</link>
      <description>&lt;P&gt;I ran the second part of the OR option and got the following error&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;37 proc sql noprint;&lt;BR /&gt;38 create table controls as&lt;BR /&gt;39 select %ut_varlist(table=bds.knee_scope,contain=_co, contain_pos=end), newdlm=%str(,))&lt;BR /&gt;-&lt;BR /&gt;22&lt;BR /&gt;200&lt;BR /&gt;WARNING: Apparent invocation of macro UT_VARLIST not resolved.&lt;BR /&gt;ERROR 22-322: Syntax error, expecting one of the following: a name, a quoted string,&lt;BR /&gt;a numeric constant, a datetime constant, a missing value, (, *, +, -, ALL, BTRIM,&lt;BR /&gt;CALCULATED, CASE, DISTINCT, EXISTS, INPUT, NOT, PUT, SUBSTRING, TRANSLATE, UNIQUE,&lt;/P&gt;</description>
      <pubDate>Wed, 06 May 2020 19:17:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-keep-variables-based-on-suffix/m-p/645709#M193049</guid>
      <dc:creator>GS2</dc:creator>
      <dc:date>2020-05-06T19:17:17Z</dc:date>
    </item>
    <item>
      <title>Re: How to keep variables based on suffix</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-keep-variables-based-on-suffix/m-p/645712#M193050</link>
      <description>&lt;P&gt;YOu need to copy the macro attached to the link &lt;A href="https://communities.sas.com/t5/SAS-Programming/Macro-function-to-return-a-list-of-variables-in-a-tab" target="_blank" rel="noopener"&gt;https://communities.sas.com/t5/SAS-Programming/Macro-function-to-return-a-list-of-variables-in-a-tab&lt;/A&gt;...&lt;BR /&gt;and submit it first&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The best would be to save ut_varlist.sas to a path referenced in the sasutos macro definitions, or to %include it&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 06 May 2020 19:28:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-keep-variables-based-on-suffix/m-p/645712#M193050</guid>
      <dc:creator>DavePrinsloo</dc:creator>
      <dc:date>2020-05-06T19:28:57Z</dc:date>
    </item>
    <item>
      <title>Re: How to keep variables based on suffix</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-keep-variables-based-on-suffix/m-p/645717#M193051</link>
      <description>Can you backwards in your step and change your naming convention so that you have prefix rather than a suffix? It seems like that change could save you a ton of time downstream.</description>
      <pubDate>Wed, 06 May 2020 19:40:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-keep-variables-based-on-suffix/m-p/645717#M193051</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-05-06T19:40:46Z</dc:date>
    </item>
  </channel>
</rss>

