<?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: Go through all datasets in a directory and delete columns that contain a specific text in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Go-through-all-datasets-in-a-directory-and-delete-columns-that/m-p/557931#M155612</link>
    <description>&lt;P&gt;SASHELP.VCOLUMN has the list of data sets in your library and all the variable names and types.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can use that to pull the table names in automatically.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It's not clear if you're dropping columns or rows.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If rows, you likely want to use FIND() to find the substr&lt;/P&gt;
&lt;P&gt;_CHARACTER_ will refer to all character variables.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If it's columns you can filter than via a SQL query and then use PROC DATASETS to drop the columns - that's the most efficient way IMO.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Get it working for one data set first and then you can generalize.&lt;/P&gt;</description>
    <pubDate>Fri, 10 May 2019 19:27:34 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2019-05-10T19:27:34Z</dc:date>
    <item>
      <title>Go through all datasets in a directory and delete columns that contain a specific text</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Go-through-all-datasets-in-a-directory-and-delete-columns-that/m-p/557920#M155606</link>
      <description>&lt;P&gt;I have about 50 datasets in my directory. This varies by study, so I need to apply the change I am asking to all the datasets in the directory.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want to delete columns that contain a string with "DTC" in it. The number of columns that contain this string could also vary. It's not consistent. There could be text before and after it, but what I want is all columns that contain "DTC" to be deleted.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So, it is a two step process:&lt;/P&gt;
&lt;P&gt;1. In each dataset, extract all "DTC" containing columns. (I know I can use index or prxmatch)&lt;/P&gt;
&lt;P&gt;2. Delete it from all datasets in the directory (this holds on to the previous list of drop variables - so unable to achieve this).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How do I go about this?&amp;nbsp;Please let me know the best way.&lt;/P&gt;
&lt;P&gt;Thank you!&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 10 May 2019 18:42:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Go-through-all-datasets-in-a-directory-and-delete-columns-that/m-p/557920#M155606</guid>
      <dc:creator>saslove</dc:creator>
      <dc:date>2019-05-10T18:42:58Z</dc:date>
    </item>
    <item>
      <title>Re: Go through all datasets in a directory and delete columns that contain a specific text</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Go-through-all-datasets-in-a-directory-and-delete-columns-that/m-p/557924#M155608</link>
      <description>&lt;P&gt;It is not clear what you are trying to do:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Are you asking to drop variables whose NAME contain the string 'DTC'?&lt;/P&gt;
&lt;P&gt;Or set the value of variables to missing where the variable value contains string 'DTC'?&lt;/P&gt;
&lt;P&gt;Or drop variables where at least one value contains the string&amp;nbsp; 'DTC'?&lt;/P&gt;</description>
      <pubDate>Fri, 10 May 2019 19:02:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Go-through-all-datasets-in-a-directory-and-delete-columns-that/m-p/557924#M155608</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-05-10T19:02:37Z</dc:date>
    </item>
    <item>
      <title>Re: Go through all datasets in a directory and delete columns that contain a specific text</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Go-through-all-datasets-in-a-directory-and-delete-columns-that/m-p/557931#M155612</link>
      <description>&lt;P&gt;SASHELP.VCOLUMN has the list of data sets in your library and all the variable names and types.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can use that to pull the table names in automatically.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It's not clear if you're dropping columns or rows.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If rows, you likely want to use FIND() to find the substr&lt;/P&gt;
&lt;P&gt;_CHARACTER_ will refer to all character variables.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If it's columns you can filter than via a SQL query and then use PROC DATASETS to drop the columns - that's the most efficient way IMO.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Get it working for one data set first and then you can generalize.&lt;/P&gt;</description>
      <pubDate>Fri, 10 May 2019 19:27:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Go-through-all-datasets-in-a-directory-and-delete-columns-that/m-p/557931#M155612</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-05-10T19:27:34Z</dc:date>
    </item>
    <item>
      <title>Re: Go through all datasets in a directory and delete columns that contain a specific text</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Go-through-all-datasets-in-a-directory-and-delete-columns-that/m-p/557936#M155615</link>
      <description>&lt;P&gt;Thanks Reeza. I tried a similar approach. The problem happening is that when I use it in a macro loop to perform the operation for all datasets in the directory, the "&amp;amp;DROPME" list holds on to the previous dataset's variables that were executed.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sql noprint;&lt;BR /&gt;SELECT name INTO :dropMe SEPARATED BY ' '&lt;BR /&gt;FROM sashelp.vcolumn&lt;BR /&gt;WHERE libname = 'TEMP' AND&lt;BR /&gt;memname = 'TOX' AND&lt;BR /&gt;INDEX(NAME,'DTC')&amp;gt;0;&lt;BR /&gt;quit;&lt;/P&gt;
&lt;P&gt;data test;&lt;BR /&gt;set temp.tox;&lt;BR /&gt;drop &amp;amp;dropme;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Fri, 10 May 2019 19:39:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Go-through-all-datasets-in-a-directory-and-delete-columns-that/m-p/557936#M155615</guid>
      <dc:creator>saslove</dc:creator>
      <dc:date>2019-05-10T19:39:46Z</dc:date>
    </item>
    <item>
      <title>Re: Go through all datasets in a directory and delete columns that contain a specific text</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Go-through-all-datasets-in-a-directory-and-delete-columns-that/m-p/557940#M155618</link>
      <description>When the SQL query does not produce any observations the target of the INTO clause is not modified.  Set if empty BEFORE the query. Or check the automatic macro variable SQLOBS to see if any were found.&lt;BR /&gt;</description>
      <pubDate>Fri, 10 May 2019 19:45:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Go-through-all-datasets-in-a-directory-and-delete-columns-that/m-p/557940#M155618</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-05-10T19:45:02Z</dc:date>
    </item>
    <item>
      <title>Re: Go through all datasets in a directory and delete columns that contain a specific text</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Go-through-all-datasets-in-a-directory-and-delete-columns-that/m-p/557972#M155632</link>
      <description>1. set Dropme to missing before theSQL query - if you don't reset it, it keeps historical values. Good practice anytime. &lt;BR /&gt;&lt;BR /&gt;2. You may not have any variables with DTC - you may also need to account for case, right now a variable named random_dtc would not be found. &lt;BR /&gt;&lt;BR /&gt;3. You can use %IF/%THEN to conditionally execute the drop. I would advise using PROC DATASETS its faster and you'll be doing this over several datasets so it's worth the time.</description>
      <pubDate>Fri, 10 May 2019 20:48:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Go-through-all-datasets-in-a-directory-and-delete-columns-that/m-p/557972#M155632</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-05-10T20:48:29Z</dc:date>
    </item>
    <item>
      <title>Re: Go through all datasets in a directory and delete columns that contain a specific text</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Go-through-all-datasets-in-a-directory-and-delete-columns-that/m-p/557980#M155639</link>
      <description>&lt;P&gt;Thanks Tom and Reeza.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is the approach I am using in the macro.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How would I do the same with proc datasets.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%macro drop (&amp;amp;dn);&lt;BR /&gt;proc sql noprint;&lt;BR /&gt;SELECT name INTO :dropMe SEPARATED BY ' '&lt;BR /&gt;FROM sashelp.vcolumn&lt;BR /&gt;WHERE libname = 'TEMP' AND&lt;BR /&gt;memname = upcase('&amp;amp;dn') AND&lt;BR /&gt;INDEX(NAME,'DTC')&amp;gt;0;&lt;BR /&gt;%let numfiles = &amp;amp;sqlObs;&lt;BR /&gt;quit;&lt;/P&gt;
&lt;P&gt;%mend drop;&lt;/P&gt;
&lt;P&gt;proc contents data=temp._all_ noprint out=contents;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data _null_;&lt;BR /&gt;set contents;&lt;BR /&gt;by memname ;&lt;BR /&gt;if first.memname;&lt;BR /&gt;call execute(cats('%drop(',memname,')'));&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks,&lt;/P&gt;</description>
      <pubDate>Fri, 10 May 2019 20:58:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Go-through-all-datasets-in-a-directory-and-delete-columns-that/m-p/557980#M155639</guid>
      <dc:creator>saslove</dc:creator>
      <dc:date>2019-05-10T20:58:45Z</dc:date>
    </item>
    <item>
      <title>Re: Go through all datasets in a directory and delete columns that contain a specific text</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Go-through-all-datasets-in-a-directory-and-delete-columns-that/m-p/557983#M155642</link>
      <description>&lt;P&gt;Nothing in that code is dropping any columns from any datasets.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;First thing to do is make sure you have a copy of the original datasets before you start dropping columns.&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;Second thing to do is make sure the first thing actually worked.&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To solve this problem I would just take the output of PROC CONTENTS and use it to generate the PROC DATASETS code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I like to generate code to a file so that I can open the file and look at it and test it before making the final version that actually runs it.&lt;/P&gt;
&lt;P&gt;Much easier to debug that either macros or CALL EXECUTE steps.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So use something like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc contents data=temp._all_ noprint out=contents;
run;
 
filename code temp;
data _null_;
  set contents;
  by memname ;
  where index(upcase(name),'DTC');
  file code;
  if first.memname then put 'modify ' memname ';' ;
  put 'drop ' name ';';
  if last.memname then put 'run;';
run;

proc dataset nolist lib=temp;
%include code / source2;
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>Fri, 10 May 2019 21:40:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Go-through-all-datasets-in-a-directory-and-delete-columns-that/m-p/557983#M155642</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-05-10T21:40:29Z</dc:date>
    </item>
    <item>
      <title>Re: Go through all datasets in a directory and delete columns that contain a specific text</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Go-through-all-datasets-in-a-directory-and-delete-columns-that/m-p/557984#M155643</link>
      <description>&lt;P&gt;Is this the actual code, or does it just resemble the code?&amp;nbsp; Issues that stand out:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;You reference a macro variable in single quotes:&amp;nbsp; '&amp;amp;dn'&amp;nbsp; &amp;nbsp;That can't work.&amp;nbsp; You need double quotes:&amp;nbsp; &lt;FONT color="#FF0000"&gt;"&amp;amp;dn"&lt;/FONT&gt;&lt;/LI&gt;
&lt;LI&gt;Variable names in sashelp.vcolumn are not necessarily in upper case.&amp;nbsp; Switch to:&amp;nbsp; &lt;FONT color="#FF0000"&gt;index(upcase(name), 'DTC') &amp;gt; 0&lt;/FONT&gt;&lt;/LI&gt;
&lt;LI&gt;Your final macro is incomplete.&amp;nbsp; Once the corrections are made, it would find a list of variable names, but wouldn't drop anything.&amp;nbsp; At the end of the macro (after the final QUIT statement, you need to add something like:&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;%if &amp;amp;sqlobs &amp;gt; 0 %then %do;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;DATA step that drops variables goes here&lt;/P&gt;
&lt;P&gt;%end;&lt;/P&gt;</description>
      <pubDate>Fri, 10 May 2019 21:51:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Go-through-all-datasets-in-a-directory-and-delete-columns-that/m-p/557984#M155643</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2019-05-10T21:51:15Z</dc:date>
    </item>
    <item>
      <title>Re: Go through all datasets in a directory and delete columns that contain a specific text</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Go-through-all-datasets-in-a-directory-and-delete-columns-that/m-p/557985#M155644</link>
      <description>&lt;P&gt;I tried it the way you suggested, but I get this error&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The DROP and KEEP statements are not supported in procedure steps in this release of the&lt;BR /&gt;SAS System. Therefore, these statements are ignored.&lt;/P&gt;</description>
      <pubDate>Fri, 10 May 2019 21:55:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Go-through-all-datasets-in-a-directory-and-delete-columns-that/m-p/557985#M155644</guid>
      <dc:creator>saslove</dc:creator>
      <dc:date>2019-05-10T21:55:52Z</dc:date>
    </item>
    <item>
      <title>Re: Go through all datasets in a directory and delete columns that contain a specific text</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Go-through-all-datasets-in-a-directory-and-delete-columns-that/m-p/557986#M155645</link>
      <description>&lt;P&gt;That was only an example of what I am trying to achieve and not the actual macro.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I tried this way, but it still holds on to the pre-resolved macro.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%macro drop (&amp;amp;dn);&lt;BR /&gt;proc sql noprint;&lt;BR /&gt;SELECT name INTO :dropMe SEPARATED BY ' '&lt;BR /&gt;FROM sashelp.vcolumn&lt;BR /&gt;WHERE libname = 'TEMP' AND&lt;BR /&gt;memname = upcase("&amp;amp;dn") AND&lt;BR /&gt;INDEX(upcase(NAME,'DTC'))&amp;gt;0;&lt;BR /&gt;quit;&lt;/P&gt;
&lt;P&gt;%if &amp;amp;sqlobs &amp;gt; 0 %then %do;&lt;BR /&gt;data test;&lt;BR /&gt;set temp.&amp;amp;dn;&lt;BR /&gt;drop &amp;amp;dropme;&lt;BR /&gt;run;&lt;BR /&gt;%end;&lt;/P&gt;
&lt;P&gt;%mend drop;&lt;/P&gt;
&lt;P&gt;proc contents data=temp._all_ noprint out=contents;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data _null_;&lt;BR /&gt;set contents;&lt;BR /&gt;by memname ;&lt;BR /&gt;if first.memname;&lt;BR /&gt;call execute(cats('%drop(',memname,')'));&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Fri, 10 May 2019 22:03:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Go-through-all-datasets-in-a-directory-and-delete-columns-that/m-p/557986#M155645</guid>
      <dc:creator>saslove</dc:creator>
      <dc:date>2019-05-10T22:03:39Z</dc:date>
    </item>
    <item>
      <title>Re: Go through all datasets in a directory and delete columns that contain a specific text</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Go-through-all-datasets-in-a-directory-and-delete-columns-that/m-p/557987#M155646</link>
      <description>&lt;P&gt;OK, let's make an earlier modification.&amp;nbsp; Now, you are using:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc contents data=temp._all_ noprint out=contents;
run;

 

data _null_;
set contents;
by memname ;
if first.memname;
call execute(cats('%drop(',memname,')'));
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Let's subset earlier, so the "drop" logic encounters data sets that must contain at least one DTC variable:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc contents data=temp._all_ noprint out=contents;
run;

 

data _null_;
set contents;
by memname ;
if first.memname;
where index(upcase(name), 'DTC') &amp;gt; 0;
call execute(cats('%drop(',memname,')'));
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;That can only help, even after the issues are diagnosed and resolved.&amp;nbsp; And it can clarify what the issues are.&lt;/P&gt;</description>
      <pubDate>Fri, 10 May 2019 22:09:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Go-through-all-datasets-in-a-directory-and-delete-columns-that/m-p/557987#M155646</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2019-05-10T22:09:56Z</dc:date>
    </item>
    <item>
      <title>Re: Go through all datasets in a directory and delete columns that contain a specific text</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Go-through-all-datasets-in-a-directory-and-delete-columns-that/m-p/557988#M155647</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4954"&gt;@Astounding&lt;/a&gt;&amp;nbsp; It's still holding to the earlier datasets DTC variables. The DTC variable name changes with each dataset.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;%macro drop (dn);&lt;BR /&gt;proc sql noprint;&lt;BR /&gt;SELECT name INTO :dropMe SEPARATED BY ' '&lt;BR /&gt;FROM sashelp.vcolumn&lt;BR /&gt;WHERE libname = 'TEMP' AND&lt;BR /&gt;memname = upcase("&amp;amp;dn") AND&lt;BR /&gt;INDEX(upcase(NAME),'DTC')&amp;gt;0;&lt;BR /&gt;quit;&lt;/P&gt;
&lt;P&gt;%if &amp;amp;sqlobs &amp;gt; 0 %then %do;&lt;BR /&gt;data test;&lt;BR /&gt;set temp.&amp;amp;dn;&lt;BR /&gt;drop &amp;amp;dropme;&lt;BR /&gt;run;&lt;BR /&gt;%end;&lt;/P&gt;
&lt;P&gt;%mend drop;&lt;/P&gt;
&lt;P&gt;proc contents data=temp._all_ noprint out=contents;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;data _null_;&lt;BR /&gt;set contents;&lt;BR /&gt;by memname ;&lt;BR /&gt;if first.memname;&lt;BR /&gt;where index(upcase(name),'DTC') &amp;gt; 0;&lt;BR /&gt;call execute(cats('%drop(',memname,')'));&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Fri, 10 May 2019 22:15:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Go-through-all-datasets-in-a-directory-and-delete-columns-that/m-p/557988#M155647</guid>
      <dc:creator>saslove</dc:creator>
      <dc:date>2019-05-10T22:15:05Z</dc:date>
    </item>
    <item>
      <title>Re: Go through all datasets in a directory and delete columns that contain a specific text</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Go-through-all-datasets-in-a-directory-and-delete-columns-that/m-p/557989#M155648</link>
      <description>&lt;P&gt;OK, time to add some temporary diagnostics, then.&amp;nbsp; Inside the macro definition:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let dropme=;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This should be the first statement within the macro.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%put &amp;amp;sqlobs;
%put &amp;amp;dropme;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This goes right after the SQL code, before checking %if &amp;amp;sqlobs &amp;gt; 0.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, my recollection is that MEMNAME is all caps within sashelp.vcolumn.&amp;nbsp; But if I'm wrong about that, the comparison should be:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;upcase(memname) = upcase("&amp;amp;dn") AND&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If this doesn't do it, sorry to say I'm done for the evening.&amp;nbsp; I'm heading out the door.&amp;nbsp; Hope this gives you enough to go on.&lt;/P&gt;</description>
      <pubDate>Fri, 10 May 2019 22:29:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Go-through-all-datasets-in-a-directory-and-delete-columns-that/m-p/557989#M155648</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2019-05-10T22:29:32Z</dc:date>
    </item>
    <item>
      <title>Re: Go through all datasets in a directory and delete columns that contain a specific text</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Go-through-all-datasets-in-a-directory-and-delete-columns-that/m-p/557990#M155649</link>
      <description>&lt;P&gt;The macro works fine if I try it on individual datasets.&lt;/P&gt;
&lt;P&gt;It gives me exactly what I need when I do a manual run on datasets&lt;/P&gt;
&lt;P&gt;%drop (bma); &amp;gt;&amp;gt; works great&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%drop (tox); &amp;gt;&amp;gt; works great&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It's only when I try the data _null_ and apply at once to all datasets, it breaks. It says no drop variables found.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 10 May 2019 22:31:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Go-through-all-datasets-in-a-directory-and-delete-columns-that/m-p/557990#M155649</guid>
      <dc:creator>saslove</dc:creator>
      <dc:date>2019-05-10T22:31:59Z</dc:date>
    </item>
    <item>
      <title>Re: Go through all datasets in a directory and delete columns that contain a specific text</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Go-through-all-datasets-in-a-directory-and-delete-columns-that/m-p/557991#M155650</link>
      <description>&lt;P&gt;Do NOT use CALL EXECUTE to call macros that use generated SAS code to generate macro variables that they same macro then uses UNLESS you add %NRSTR() around the macro call.&lt;BR /&gt;Otherwise the timing of creating the macro variable and referencing it will be off. The SAS code the macro generates will be output when the CALL EXECUTE() runs but that SAS code will not execute until after the data step finishes.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So if you run the code once and created global macro variables DROPME and SQLOBS and&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let dropme=var1 var2;
%let sqlobs=2;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;then run this data step:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  call execute('%drop(AAA)');
  call execute('%drop(BBB)');
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then&amp;nbsp; no matter what variables are in AAA or BBB because SQLOBS is non zero NOW what will be pushed onto the stack to run will be SAS code like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql .... select name into :dropme ....
where memname="AAA"....
quit;
data test;
  set temp.AAA;
  drop var1 var2;
run;
proc sql .... select name into :dropme ....
where memname="BBB"....
quit;
data test;
  set temp.BBB;
  drop var1 var2;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Yet another reason NOT to use CALL EXECUTE().&lt;/P&gt;
&lt;P&gt;If you must use it then add %NRSTR() like so:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  call execute('%nrstr(%drop)(AAA)');&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then what is pushed onto the stack to run after the data step is instead the actual call to the macro.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%drop(AAA)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Which will now work since the SQL code will run BEFORE the decision is mode for who to generate the DROP statement instead of after the DROP statement has already been generated.&lt;/P&gt;</description>
      <pubDate>Fri, 10 May 2019 23:09:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Go-through-all-datasets-in-a-directory-and-delete-columns-that/m-p/557991#M155650</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-05-10T23:09:54Z</dc:date>
    </item>
    <item>
      <title>Re: Go through all datasets in a directory and delete columns that contain a specific text</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Go-through-all-datasets-in-a-directory-and-delete-columns-that/m-p/557993#M155652</link>
      <description>&lt;P&gt;Thanks for the tip. Could you guide me through how I would apply this step to all datasets in my directory. If I don't use call execute, what would I do here instead?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%macro drop (dn);&lt;BR /&gt;%let dropme=;&lt;BR /&gt;proc sql noprint;&lt;BR /&gt;SELECT name INTO :dropMe SEPARATED BY ' '&lt;BR /&gt;FROM sashelp.vcolumn&lt;BR /&gt;WHERE libname = 'TEMP' AND&lt;BR /&gt;memname = upcase("&amp;amp;dn") AND&lt;BR /&gt;INDEX(upcase(NAME),'DTC')&amp;gt;0;&lt;BR /&gt;quit;&lt;/P&gt;
&lt;P&gt;%put &amp;amp;sqlobs;&lt;BR /&gt;%put &amp;amp;dropme;&lt;/P&gt;
&lt;P&gt;%if &amp;amp;sqlobs &amp;gt; 0 %then %do;&lt;BR /&gt;data test;&lt;BR /&gt;set temp.&amp;amp;dn;&lt;BR /&gt;drop &amp;amp;dropme;&lt;BR /&gt;run;&lt;BR /&gt;%end;&lt;/P&gt;
&lt;P&gt;%mend drop;&lt;/P&gt;
&lt;P&gt;%drop (bma);&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;proc contents data=temp._all_ noprint out=contents;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;data _null_;&lt;BR /&gt;set contents;&lt;BR /&gt;by memname ;&lt;BR /&gt;where index(upcase(name),'DTC') &amp;gt; 0;&lt;BR /&gt;call execute(cats('%drop(',memname,')'));&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Fri, 10 May 2019 23:08:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Go-through-all-datasets-in-a-directory-and-delete-columns-that/m-p/557993#M155652</guid>
      <dc:creator>saslove</dc:creator>
      <dc:date>2019-05-10T23:08:02Z</dc:date>
    </item>
    <item>
      <title>Re: Go through all datasets in a directory and delete columns that contain a specific text</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Go-through-all-datasets-in-a-directory-and-delete-columns-that/m-p/557994#M155653</link>
      <description>&lt;P&gt;If you keep the macro that way then you need to keep the FIRST.MEMNAME subset or else you will call the macro once for every variable you want to drop, but only the first call is needed since it will drop all of the variables for that table.&lt;/P&gt;
&lt;P&gt;You didn't add the %NRSTR() to fix the timing issue with using CALL EXECUTE().&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  set contents;
  by memname ;
  where index(upcase(name),'DTC') ;
  if first.memname;
  call execute(cats('%nrstr(%drop)(',memname,')'));
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 11 May 2019 16:14:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Go-through-all-datasets-in-a-directory-and-delete-columns-that/m-p/557994#M155653</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-05-11T16:14:23Z</dc:date>
    </item>
    <item>
      <title>Re: Go through all datasets in a directory and delete columns that contain a specific text</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Go-through-all-datasets-in-a-directory-and-delete-columns-that/m-p/557996#M155655</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/1717"&gt;@saslove&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Below a slightly different coding approach but fully working code for the generated sample data.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* create sample data */
data 
  ds1(drop=varDTC01 DTC _dTc_xx)
  ds2(keep=name sex aaDtXc_06)
  ds3
  ;
  length varDTC01 DTC _dTc_xx aaDtXc_06 abc_dtc abcDtcX $1;
  if 0 then call missing(of _all_);
  set sashelp.class;
run;

/* define parameters */
%let libref=work;
%let col_pattern=DTC;

/* 
  generate SAS code for dropping columns 
  write generated code to temporary file codegen 
*/
filename codegen temp;
data _null_;
/*  file print;*/
  file codegen;
  set sashelp.vcolumn(keep=libname memname name where=(libname=%upcase("&amp;amp;libref") and find(name,"&amp;amp;col_pattern",'i')&amp;gt;0));
  by libname memname;
  if first.memname then
    do;
      put 
        / 'proc sql;'
        / '  alter table ' libname +(-1) '.' memname
        / '    drop ' name
        @;
    end;
  else
    do;
      put ',' name @@;
    end;
  if last.memname then
    do;
      put
        / '  ;'
        / 'quit;'
        ;
    end;
run;

/* ensure file codegen gets create even if above step didn't select any rows */
data _null_;
  file codegen mod;
/*  put;*/
  stop;
run;

/* execute generated code */
%include codegen / source2;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;On a side note: A data step with a keep statement recreates your tables and you would loose any indexes and constraints (if any). A SQL ALTER TABLE drops the columns from your source table and though will not change anything else.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For testing: Uncomment FILE PRINT but comment FILE CODEGEN; and %INCLUDE .... This will print the generated code without executing it.&lt;/P&gt;</description>
      <pubDate>Sat, 11 May 2019 03:48:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Go-through-all-datasets-in-a-directory-and-delete-columns-that/m-p/557996#M155655</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2019-05-11T03:48:09Z</dc:date>
    </item>
    <item>
      <title>Re: Go through all datasets in a directory and delete columns that contain a specific text</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Go-through-all-datasets-in-a-directory-and-delete-columns-that/m-p/557997#M155656</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/1717"&gt;@saslove&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here an approach using call execute().&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* create sample data */
data 
  ds1(drop=varDTC01 DTC _dTc_xx)
  ds2(keep=name sex aaDtXc_06)
  ds3
  ;
  length varDTC01 DTC _dTc_xx aaDtXc_06 abc_dtc abcDtcX $1;
  if 0 then call missing(of _all_);
  set sashelp.class;
run;

/* define parameters */
%let libref=work;
%let col_pattern=DTC;

/* 
  use call execute to drop a list of columns dynamically populated into SAS variable DropList
*/
data _null_;
  set sashelp.vcolumn(keep=libname memname name where=(libname=%upcase("&amp;amp;libref") and find(name,"&amp;amp;col_pattern",'i')&amp;gt;0));
  by libname memname;
  length DropList $32767;
  retain DropList;
  DropList=catx(',',DropList,name);
  if last.memname then
    do;
      call execute(catx(' ', 'proc sql; alter table', cats(libname,'.',memname),'drop', DropList, ';quit;'));
      call missing(DropList);
    end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Fixed as per&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt;'s comment.&lt;/P&gt;</description>
      <pubDate>Sun, 12 May 2019 02:10:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Go-through-all-datasets-in-a-directory-and-delete-columns-that/m-p/557997#M155656</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2019-05-12T02:10:55Z</dc:date>
    </item>
  </channel>
</rss>

