<?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 Dropping a variable from a dataset conditionally in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Dropping-a-variable-from-a-dataset-conditionally/m-p/518010#M140145</link>
    <description>&lt;P&gt;I am trying to conditionally drop variables from a data set.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro drop_correlated(data_set);
%macro dummy; %mend dummy;

	proc corr data=&amp;amp;data_set. pearson NOPROB nomiss Rank NoProb outp=correlations;                                           
	run;

	data reduced;
		set correlations;
		where _TYPE_ = "CORR";
	run;

	data reduced;
		set reduced;
		drop _TYPE_;
	run;

	data _null_;
		set reduced;
		call symput(cat('VAR_',_N_),compress(trim(left(_NAME_))));
		call symput('NUM_VAR',_N_);
	run;
		
	%local i;
	%do i=1 %to &amp;amp;NUM_VAR.;

		data temp_&amp;amp;i.;
			set reduced;
			where _name_ = "&amp;amp;&amp;amp;VAR_&amp;amp;i.";
			drop _name_;
		run;

		%local j;
		%do j=1 %to &amp;amp;NUM_VAR.;

			data temp_&amp;amp;i.;
				set temp_&amp;amp;i.;
				if (-0.5 &amp;lt;= &amp;amp;&amp;amp;VAR_&amp;amp;j. &amp;lt;= 0.5) then %drop_var(temp_&amp;amp;i, &amp;amp;&amp;amp;VAR_&amp;amp;j.);
			run;
		%end;
	%end;
	%put _ALL_;

%mend drop_correlated;

%macro drop_var(data_in, var);
%macro dummy; %mend dummy;

	data &amp;amp;data_in.;
		set &amp;amp;data_in.;
		drop &amp;amp;var.;
	run;

%mend drop_var;


%drop_correlated(data);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I get the error:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ERROR 180-322: Statement is not valid or it is used out of proper order.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I think this is coming from the drop_var macro.&lt;/P&gt;</description>
    <pubDate>Mon, 03 Dec 2018 11:03:38 GMT</pubDate>
    <dc:creator>meighanj</dc:creator>
    <dc:date>2018-12-03T11:03:38Z</dc:date>
    <item>
      <title>Dropping a variable from a dataset conditionally</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dropping-a-variable-from-a-dataset-conditionally/m-p/518010#M140145</link>
      <description>&lt;P&gt;I am trying to conditionally drop variables from a data set.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro drop_correlated(data_set);
%macro dummy; %mend dummy;

	proc corr data=&amp;amp;data_set. pearson NOPROB nomiss Rank NoProb outp=correlations;                                           
	run;

	data reduced;
		set correlations;
		where _TYPE_ = "CORR";
	run;

	data reduced;
		set reduced;
		drop _TYPE_;
	run;

	data _null_;
		set reduced;
		call symput(cat('VAR_',_N_),compress(trim(left(_NAME_))));
		call symput('NUM_VAR',_N_);
	run;
		
	%local i;
	%do i=1 %to &amp;amp;NUM_VAR.;

		data temp_&amp;amp;i.;
			set reduced;
			where _name_ = "&amp;amp;&amp;amp;VAR_&amp;amp;i.";
			drop _name_;
		run;

		%local j;
		%do j=1 %to &amp;amp;NUM_VAR.;

			data temp_&amp;amp;i.;
				set temp_&amp;amp;i.;
				if (-0.5 &amp;lt;= &amp;amp;&amp;amp;VAR_&amp;amp;j. &amp;lt;= 0.5) then %drop_var(temp_&amp;amp;i, &amp;amp;&amp;amp;VAR_&amp;amp;j.);
			run;
		%end;
	%end;
	%put _ALL_;

%mend drop_correlated;

%macro drop_var(data_in, var);
%macro dummy; %mend dummy;

	data &amp;amp;data_in.;
		set &amp;amp;data_in.;
		drop &amp;amp;var.;
	run;

%mend drop_var;


%drop_correlated(data);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I get the error:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ERROR 180-322: Statement is not valid or it is used out of proper order.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I think this is coming from the drop_var macro.&lt;/P&gt;</description>
      <pubDate>Mon, 03 Dec 2018 11:03:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dropping-a-variable-from-a-dataset-conditionally/m-p/518010#M140145</guid>
      <dc:creator>meighanj</dc:creator>
      <dc:date>2018-12-03T11:03:38Z</dc:date>
    </item>
    <item>
      <title>Re: Dropping a variable from a dataset conditionally</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dropping-a-variable-from-a-dataset-conditionally/m-p/518011#M140146</link>
      <description>&lt;P&gt;You are trying to call a data step conditionally from a data step.&amp;nbsp; You can't do that.&lt;/P&gt;</description>
      <pubDate>Mon, 03 Dec 2018 11:21:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dropping-a-variable-from-a-dataset-conditionally/m-p/518011#M140146</guid>
      <dc:creator>WarrenKuhfeld</dc:creator>
      <dc:date>2018-12-03T11:21:39Z</dc:date>
    </item>
    <item>
      <title>Re: Dropping a variable from a dataset conditionally</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dropping-a-variable-from-a-dataset-conditionally/m-p/518013#M140147</link>
      <description>&lt;P&gt;What you want to do is nonsense, your code would be equivalent to&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data temp;
set temp;
if (-0.5 &amp;lt;= var &amp;lt;= 0.5) then drop var;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Drop is a non-conditional statement. You can't drop a variable for a specific row, only for the whole dataset.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 03 Dec 2018 11:32:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dropping-a-variable-from-a-dataset-conditionally/m-p/518013#M140147</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-12-03T11:32:37Z</dc:date>
    </item>
    <item>
      <title>Re: Dropping a variable from a dataset conditionally</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dropping-a-variable-from-a-dataset-conditionally/m-p/518015#M140148</link>
      <description>&lt;P&gt;The data set only has one row though.&lt;/P&gt;</description>
      <pubDate>Mon, 03 Dec 2018 11:34:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dropping-a-variable-from-a-dataset-conditionally/m-p/518015#M140148</guid>
      <dc:creator>meighanj</dc:creator>
      <dc:date>2018-12-03T11:34:06Z</dc:date>
    </item>
    <item>
      <title>Re: Dropping a variable from a dataset conditionally</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dropping-a-variable-from-a-dataset-conditionally/m-p/518017#M140149</link>
      <description>&lt;P&gt;Please post example test data in the form of a datastep:&lt;/P&gt;
&lt;P&gt;&lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712" target="_blank"&gt;https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And show what you want out at the end.&amp;nbsp; i would guess a data _null_ to create the list then a drop would be best, something like (assumes missing is to be dropped):&lt;/P&gt;
&lt;PRE&gt;data _null_;
  length dlist $2000;
  set have;
  array c{*} _character_;
  array n{*} _numeric_;
  do over c;
    if missing(c) then dlist=catx(" ",dlist,c);
  end;
  do over n;
    if missing(n) then dlist=catx(" ",dlist,n);
  end;
  call symputx('dlist',dlist);
run;
data want;
  set want (drop=&amp;amp;dlist.);
run;
  &lt;/PRE&gt;</description>
      <pubDate>Mon, 03 Dec 2018 11:47:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dropping-a-variable-from-a-dataset-conditionally/m-p/518017#M140149</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-12-03T11:47:23Z</dc:date>
    </item>
  </channel>
</rss>

