<?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: delete col if exist in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/delete-col-if-exist/m-p/970784#M377162</link>
    <description>&lt;P&gt;Query DICTIONARY.COLUMNS with a suitable WHERE clause in a SELECT INTO to create the variable list which you can then use in a DROP statement or DROP= dataset option.&lt;/P&gt;</description>
    <pubDate>Tue, 15 Jul 2025 11:28:08 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2025-07-15T11:28:08Z</dc:date>
    <item>
      <title>delete col if exist</title>
      <link>https://communities.sas.com/t5/SAS-Programming/delete-col-if-exist/m-p/970781#M377159</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;I want to delete (DROP) columns if they exist.&lt;/P&gt;
&lt;P&gt;For example: I want to drop columns make and model if they exist.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data cars;
set sashelp.cars;
drop make;
Run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 15 Jul 2025 11:11:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/delete-col-if-exist/m-p/970781#M377159</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2025-07-15T11:11:10Z</dc:date>
    </item>
    <item>
      <title>Re: delete col if exist</title>
      <link>https://communities.sas.com/t5/SAS-Programming/delete-col-if-exist/m-p/970783#M377161</link>
      <description>&lt;P&gt;Do you want to remove the column from an existing dataset or do you want to clone a dataset skipping one variable?&lt;/P&gt;</description>
      <pubDate>Tue, 15 Jul 2025 11:19:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/delete-col-if-exist/m-p/970783#M377161</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2025-07-15T11:19:40Z</dc:date>
    </item>
    <item>
      <title>Re: delete col if exist</title>
      <link>https://communities.sas.com/t5/SAS-Programming/delete-col-if-exist/m-p/970784#M377162</link>
      <description>&lt;P&gt;Query DICTIONARY.COLUMNS with a suitable WHERE clause in a SELECT INTO to create the variable list which you can then use in a DROP statement or DROP= dataset option.&lt;/P&gt;</description>
      <pubDate>Tue, 15 Jul 2025 11:28:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/delete-col-if-exist/m-p/970784#M377162</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2025-07-15T11:28:08Z</dc:date>
    </item>
    <item>
      <title>Re: delete col if exist</title>
      <link>https://communities.sas.com/t5/SAS-Programming/delete-col-if-exist/m-p/970786#M377164</link>
      <description>In existing data set delete the col if it exist</description>
      <pubDate>Tue, 15 Jul 2025 11:52:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/delete-col-if-exist/m-p/970786#M377164</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2025-07-15T11:52:31Z</dc:date>
    </item>
    <item>
      <title>Re: delete col if exist</title>
      <link>https://communities.sas.com/t5/SAS-Programming/delete-col-if-exist/m-p/970787#M377165</link>
      <description>&lt;P&gt;Here is a macro example that allows you to check if a variable exists in a data set:&lt;/P&gt;
&lt;P&gt;&lt;A href="https://support.sas.com/kb/25/082.html" target="_blank"&gt;https://support.sas.com/kb/25/082.html&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If the variable does not exist, the value of &amp;amp;val is 0. Otherwise, the value is the variable's position in the data set.&lt;/P&gt;</description>
      <pubDate>Tue, 15 Jul 2025 12:06:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/delete-col-if-exist/m-p/970787#M377165</guid>
      <dc:creator>Kathryn_SAS</dc:creator>
      <dc:date>2025-07-15T12:06:29Z</dc:date>
    </item>
    <item>
      <title>Re: delete col if exist</title>
      <link>https://communities.sas.com/t5/SAS-Programming/delete-col-if-exist/m-p/970788#M377166</link>
      <description>&lt;P&gt;If you set system option drkrocond=nowarn, you can list the variables that don't exist on the DROP statement, and they will be ignored.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;1    option dkrocond=nowarn ;
2
3    data cars ;
4      set sashelp.cars ;
5      drop make notthere;
6    run ;

NOTE: There were 428 observations read from the data set SASHELP.CARS.
NOTE: The data set WORK.CARS has 428 observations and 14 variables.

7
8    option dkrocond=warn
&lt;/PRE&gt;</description>
      <pubDate>Tue, 15 Jul 2025 12:22:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/delete-col-if-exist/m-p/970788#M377166</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2025-07-15T12:22:46Z</dc:date>
    </item>
    <item>
      <title>Re: delete col if exist</title>
      <link>https://communities.sas.com/t5/SAS-Programming/delete-col-if-exist/m-p/970789#M377167</link>
      <description>&lt;P&gt;Small macro, just for fun;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
%macro dropVariableIfExist(
  ds /* name of data set with variables to drop */
, vars /* space separated list of variables to drop */
, out=work.want /* name of result data set */
);
  %local sentinel rc;
  %let sentinel=_sentinel_%sysfunc(datetime(),b8601dt15.)_;
  %let rc = %sysfunc(doSubL(%str(
    options nomprint nosymbolgen nomlogic msglevel=N nosource ps=min nonotes;
    data _null_;
      &amp;amp;sentinel._1_=.;
      if 0 then set &amp;amp;ds.;
      &amp;amp;sentinel._2_=" ";

      array _numeric_(&amp;amp;sentinel._I_) _numeric_;
      array _character_(&amp;amp;sentinel._I_) _character_;

      length &amp;amp;sentinel._3_ $ 32767;
      &amp;amp;sentinel._3_=" ";

      length &amp;amp;sentinel._K_ $ 32;
      &amp;amp;sentinel._K_ = "*";
      declare hash &amp;amp;sentinel._H_();
      &amp;amp;sentinel._H_.defineKey("&amp;amp;sentinel._K_");
      &amp;amp;sentinel._H_.defineDone();

      do _N_ = 1 by 1 while(&amp;amp;sentinel._K_ NE " ");
        &amp;amp;sentinel._K_ = upcase(scan(symget("vars"),_N_," "));
        &amp;amp;sentinel._H_.REPLACE(KEY:&amp;amp;sentinel._K_
                             ,DATA:&amp;amp;sentinel._K_
                             );
      end;

      do over _numeric_;
        if 0=&amp;amp;sentinel._H_.CHECK(KEY:upcase(strip(vname(_numeric_)))) then 
          &amp;amp;sentinel._3_=catX(" ", &amp;amp;sentinel._3_, vname(_numeric_)); 
      end;
      do over _character_;
        if 0=&amp;amp;sentinel._H_.CHECK(KEY:upcase(strip(vname(_character_)))) then 
          &amp;amp;sentinel._3_=catX(" ", &amp;amp;sentinel._3_, vname(_character_)); 
      end;

      
      if &amp;amp;sentinel._3_ NE " " then
        do;
          put "INFO: The following variables: " &amp;amp;sentinel._3_ "will be dropped.";
          call execute("options notes source; data " !! symget("out") !! ";");
          call execute(" set "  !! symget("ds")  !! ";");
          call execute(" drop ");
          call execute(&amp;amp;sentinel._3_);
          call execute("; run;");
        end;
      else put "INFO: No variables to drop.";
      /* put _ALL_; */
      stop;
    run;
  )));
%mend dropVariableIfExist;

options mprint;
%dropVariableIfExist(sashelp.cars, make model)

%put ##%dropVariableIfExist(sashelp.cars, NOmake NOmodel, out=work.want2)##;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;It runs as pure macrocode &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Tue, 15 Jul 2025 12:32:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/delete-col-if-exist/m-p/970789#M377167</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2025-07-15T12:32:21Z</dc:date>
    </item>
    <item>
      <title>Re: delete col if exist</title>
      <link>https://communities.sas.com/t5/SAS-Programming/delete-col-if-exist/m-p/970790#M377168</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/19879"&gt;@Quentin&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;macro version, no need for options change in main session thanks to DoSubL() &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro dropVariableIfExistQ(
  ds /* name of data set with variables to drop */
, vars /* space separated list of variables to drop */
, out=work.want /* name of result data set */
);
  %local rc;
  %let rc = %sysfunc(doSubL(%str(
    options nomprint nosymbolgen nomlogic msglevel=N nosource ps=min notes;
    option dkrocond=nowarn;

    data &amp;amp;out.;
      set &amp;amp;ds.;
      drop
        &amp;amp;vars.
      ;
      run;
  )));
%mend dropVariableIfExistQ;

options mprint;
%dropVariableIfExistQ(sashelp.cars, make model)

%put ##%dropVariableIfExistQ(sashelp.cars, NOmake NOmodel, out=work.want2)##;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Tue, 15 Jul 2025 12:36:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/delete-col-if-exist/m-p/970790#M377168</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2025-07-15T12:36:09Z</dc:date>
    </item>
    <item>
      <title>Re: delete col if exist</title>
      <link>https://communities.sas.com/t5/SAS-Programming/delete-col-if-exist/m-p/970793#M377171</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/19879"&gt;@Quentin&lt;/a&gt;&amp;nbsp;, expanding your idea a little bit:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro bpUTiL_cleanDropKeep(
  /* idea by Quentin McMullen */
  ds /* name of data set with variables to drop */
, drop= /* space separated list of variables to drop */
, keep= /* space separated list of variables to keep */
, out=work.want /* name of result data set */
);
  %local rc;
  %put NOTE-;
  %let rc = %sysfunc(doSubL(%nrstr(
    options nomprint nosymbolgen nomlogic msglevel=N nosource ps=min notes;
    option dkrocond=nowarn;
    data &amp;amp;out.;
      set &amp;amp;ds.;
      %sysfunc(ifc(%sysevalf(%superq(drop) NE ,boolean)=1, %str(drop &amp;amp;drop.), %str( ), ));
      %sysfunc(ifc(%sysevalf(%superq(keep) NE ,boolean)=1, %str(keep &amp;amp;keep.), %str( ), ));
      run;
  )));
  %put NOTE-;
%mend bpUTiL_cleanDropKeep;

options mprint;
%bpUTiL_cleanDropKeep(sashelp.cars, drop=make model)

%put ##%bpUTiL_cleanDropKeep(sashelp.cars, keep=NOmake NOmodel, out=work.want2)##;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I think I'll add it to bpUTiL package &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;[EDIT:] I've added Rename to have it all:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro bpUTiL_cleanDropKeep(
  /* idea by Quentin McMullen */
  ds /* name of data set with variables to drop */
, drop= /* space separated list of variables to drop */
, keep= /* space separated list of variables to keep */
, rename= /* list of variables pairs to rename */
, out=work.want /* name of result data set */
);
  %local rc;
  %put NOTE-;
  %let rc = %sysfunc(doSubL(%nrstr(
    options nomprint nosymbolgen nomlogic msglevel=N nosource ps=min notes;
    option dkrocond=nowarn;
    data &amp;amp;out.;
      set &amp;amp;ds.;
      %sysfunc(ifc(%sysevalf(%superq(drop) NE ,boolean)=1, %str(drop &amp;amp;drop.), , ));
      %sysfunc(ifc(%sysevalf(%superq(keep) NE ,boolean)=1, %str(keep &amp;amp;keep.), , ));
      %sysfunc(ifc(%sysevalf(%superq(rename) NE ,boolean)=1, %str(rename &amp;amp;rename.), , ));
      run;
  )));
  %put NOTE-;
%mend bpUTiL_cleanDropKeep;

options mprint;
%bpUTiL_cleanDropKeep(sashelp.cars, drop=make model)

%put ##%bpUTiL_cleanDropKeep(sashelp.cars, keep=NOmake NOmodel, out=work.want2)##;

%bpUTiL_cleanDropKip(sashelp.cars,keep=make ABC, rename=ABC=EFG make=make_make_make)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Tue, 15 Jul 2025 13:19:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/delete-col-if-exist/m-p/970793#M377171</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2025-07-15T13:19:43Z</dc:date>
    </item>
    <item>
      <title>Re: delete col if exist</title>
      <link>https://communities.sas.com/t5/SAS-Programming/delete-col-if-exist/m-p/970795#M377172</link>
      <description>&lt;P&gt;Nice,&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/35763"&gt;@yabwon&lt;/a&gt;&amp;nbsp;.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I actually picked up the idea of using DKROCOND that way from SAS guru Ed Heaton, who wrote about it here:&lt;/P&gt;
&lt;P&gt;&lt;A href="https://support.sas.com/resources/papers/proceedings/proceedings/sugi28/067-28.pdf" target="_blank"&gt;https://support.sas.com/resources/papers/proceedings/proceedings/sugi28/067-28.pdf&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As mentioned in the paper, he was a fan of putting drop=_:&amp;nbsp; in every DATA step he wrote.&lt;/P&gt;</description>
      <pubDate>Tue, 15 Jul 2025 13:09:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/delete-col-if-exist/m-p/970795#M377172</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2025-07-15T13:09:39Z</dc:date>
    </item>
  </channel>
</rss>

