<?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: Removing a variable conditionally in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Removing-a-variable-conditionally/m-p/427523#M105458</link>
    <description>&lt;P&gt;To remove a variable you need to either DROP it or not KEEP it.&amp;nbsp; Basically you need use code generation create the DROP statement (or DROP= dataset option.&amp;nbsp; One easy way to do that is to put the variable name into a macro variable and then use the value of the macro variable in your code.&amp;nbsp; You could look on-line for many examples of how to do this for variables with all missing values and just adopt one and modify it to work for variables that are all one.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is one way.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;
  input var1-var4 ;
cards;
0.5  1  1.2  0.4
0.9  1  1.5  0.2
0.7  1  1.1  0.3
1    1  1    1
;

%let droplist=;
data _null_;
  array _flags (100) _temporary_ (100*1) ;
  set have end=eof;
  array _in  var1-var4 ;
  do _n_ =1 to dim(_in) ;
    if _in(_n_) ne 1 then _flags(_n_)=0;
  end;
  if eof then do _n_=1 to dim(_in);
    if _flags(_n_) then call execute(catx(' ','%let droplist=&amp;amp;droplist',nliteral(vname(_in(_n_))),';'));
  end;
run;
%put &amp;amp;=droplist ;
data want ;
  set have ;
  drop &amp;amp;droplist;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you have more than 100 variables you want to test then just increase the size of the temporary array.&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;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sun, 14 Jan 2018 17:32:05 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2018-01-14T17:32:05Z</dc:date>
    <item>
      <title>Removing a variable conditionally</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Removing-a-variable-conditionally/m-p/427519#M105456</link>
      <description>&lt;P&gt;Hi everyone!&lt;/P&gt;&lt;P&gt;I'm having a little trouble with a data set. I've the idea of dropping a variable if all the observations have the value of 1,&lt;/P&gt;&lt;P&gt;The table is like this;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;var1 var2 var3 var4&lt;/P&gt;&lt;P&gt;0.5&amp;nbsp; 1&amp;nbsp; 1.2&amp;nbsp; 0.4&lt;/P&gt;&lt;P&gt;0.9&amp;nbsp; 1&amp;nbsp; 1.5&amp;nbsp; 0.2&lt;/P&gt;&lt;P&gt;0.7&amp;nbsp; 1 &amp;nbsp; 1.1&amp;nbsp; 0.3&lt;/P&gt;&lt;P&gt;1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1 &amp;nbsp;&amp;nbsp; 1 &amp;nbsp;&amp;nbsp; 1&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to drop the variable var2 in this case. My problem is that I can't do it mannualy, as the four variables are being included because of a macro, and the name of them can change.&lt;/P&gt;&lt;P&gt;Is there any way to remove it conditionally?&lt;/P&gt;&lt;P&gt;Thanks in advance.&lt;/P&gt;</description>
      <pubDate>Sun, 14 Jan 2018 16:35:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Removing-a-variable-conditionally/m-p/427519#M105456</guid>
      <dc:creator>fabBot</dc:creator>
      <dc:date>2018-01-14T16:35:04Z</dc:date>
    </item>
    <item>
      <title>Re: Removing a variable conditionally</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Removing-a-variable-conditionally/m-p/427523#M105458</link>
      <description>&lt;P&gt;To remove a variable you need to either DROP it or not KEEP it.&amp;nbsp; Basically you need use code generation create the DROP statement (or DROP= dataset option.&amp;nbsp; One easy way to do that is to put the variable name into a macro variable and then use the value of the macro variable in your code.&amp;nbsp; You could look on-line for many examples of how to do this for variables with all missing values and just adopt one and modify it to work for variables that are all one.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is one way.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;
  input var1-var4 ;
cards;
0.5  1  1.2  0.4
0.9  1  1.5  0.2
0.7  1  1.1  0.3
1    1  1    1
;

%let droplist=;
data _null_;
  array _flags (100) _temporary_ (100*1) ;
  set have end=eof;
  array _in  var1-var4 ;
  do _n_ =1 to dim(_in) ;
    if _in(_n_) ne 1 then _flags(_n_)=0;
  end;
  if eof then do _n_=1 to dim(_in);
    if _flags(_n_) then call execute(catx(' ','%let droplist=&amp;amp;droplist',nliteral(vname(_in(_n_))),';'));
  end;
run;
%put &amp;amp;=droplist ;
data want ;
  set have ;
  drop &amp;amp;droplist;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you have more than 100 variables you want to test then just increase the size of the temporary array.&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;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 14 Jan 2018 17:32:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Removing-a-variable-conditionally/m-p/427523#M105458</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-01-14T17:32:05Z</dc:date>
    </item>
    <item>
      <title>Re: Removing a variable conditionally</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Removing-a-variable-conditionally/m-p/427532#M105465</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you are comfortable with some statistical stuff then try this.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc means data=have stackods var;&lt;BR /&gt;ods output summary=stats;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;proc sql;&lt;BR /&gt;select Variable into :drop_vars from stats&lt;BR /&gt;where Var=0;&lt;BR /&gt;quit;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;BR /&gt;set have(drop=&amp;amp;drop_vars);&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Sun, 14 Jan 2018 18:26:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Removing-a-variable-conditionally/m-p/427532#M105465</guid>
      <dc:creator>stat_sas</dc:creator>
      <dc:date>2018-01-14T18:26:47Z</dc:date>
    </item>
    <item>
      <title>Re: Removing a variable conditionally</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Removing-a-variable-conditionally/m-p/427565#M105480</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/42042"&gt;@stat_sas&lt;/a&gt;Nice use of the ODS OUTPUT statement.&amp;nbsp; If more than one variable needs to be dropped a SEPARATED BY phrase could be added to the SQL step which creates &amp;amp;DROP_VARS:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;select Variable into :drop_vars separated by ' ' from stats
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Also this will eliminate any variable with constant value, other than all missing, not just all 1's.&lt;/P&gt;</description>
      <pubDate>Sun, 14 Jan 2018 22:37:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Removing-a-variable-conditionally/m-p/427565#M105480</guid>
      <dc:creator>ArtC</dc:creator>
      <dc:date>2018-01-14T22:37:58Z</dc:date>
    </item>
    <item>
      <title>Re: Removing a variable conditionally</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Removing-a-variable-conditionally/m-p/427652#M105520</link>
      <description>&lt;P&gt;I really don't understand very well what it's being doing in the _null_ data set, but it worked perfectly ! Thank you very much, i'll try to study it.&lt;/P&gt;</description>
      <pubDate>Mon, 15 Jan 2018 11:51:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Removing-a-variable-conditionally/m-p/427652#M105520</guid>
      <dc:creator>fabBot</dc:creator>
      <dc:date>2018-01-15T11:51:12Z</dc:date>
    </item>
    <item>
      <title>Re: Removing a variable conditionally</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Removing-a-variable-conditionally/m-p/427672#M105523</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;
  input var1-var4 ;
cards;
0.5  1  1.2  0.4
0.9  1  1.5  0.2
0.7  1  1.1  0.3
1    1  1    1
;

proc sql noprint;
select catx(' ','sum(',name,'=1) as ',name)  into : list	separated by ','
 from dictionary.columns
  where libname='WORK' and memname='HAVE';
create table temp as
 select &amp;amp;list from have;
quit;
proc transpose data=temp out=temp1;
run;
proc sql noprint;
select count(*) into : n from have;
select _name_ into : drop separated by ','
 from temp1
  where col1=&amp;amp;n;
alter table have
 drop &amp;amp;drop;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 15 Jan 2018 13:21:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Removing-a-variable-conditionally/m-p/427672#M105523</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2018-01-15T13:21:31Z</dc:date>
    </item>
    <item>
      <title>Re: Removing a variable conditionally</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Removing-a-variable-conditionally/m-p/427677#M105524</link>
      <description>&lt;P&gt;The simplest way is using IML .&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;
  input var1-var4 ;
cards;
0.5  1  1.2  0.4
0.9  1  1.5  0.2
0.7  1  1.1  0.3
1    1  1    1
;
proc iml;
use have nobs nobs;
read all var _all_ into x[c=vname];
close;
drop=vname[loc((x=1)[+,]=nobs)];  
submit drop;
 data want;
  set have;
  drop &amp;amp;drop;
 run;
endsubmit;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 15 Jan 2018 13:36:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Removing-a-variable-conditionally/m-p/427677#M105524</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2018-01-15T13:36:48Z</dc:date>
    </item>
  </channel>
</rss>

