<?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: Getting rid of a variable, conditionally. in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Getting-rid-of-a-variable-conditionally/m-p/537958#M148045</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; 

Data have;
   do i =1 to 5;
   if i=1 then do; a="1"; b="2";c=""; end;
   if i=2 then do; a="3"; b="4";c="0"; end;
   if i=3 then do; a="5"; b="6"; c=""; end;
   if i=4 then do; a="7"; b="8"; c="0"; end;
   if i=5 then do; a="9"; b="10"; c=""; end;
   output;
   end;
run;
proc sql;
create table temp as
 select sum(a not in (' ' '0')) as a,
 sum(b not in (' ' '0')) as b,
 sum(c not in (' ' '0')) as c
 from have;
quit;
proc transpose data=temp out=temp1;
run;
proc sql;
select _name_ into : list separated by ',' from temp1
 where col1=0;

alter table have
drop &amp;amp;list;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Sat, 23 Feb 2019 11:59:29 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2019-02-23T11:59:29Z</dc:date>
    <item>
      <title>Getting rid of a variable, conditionally.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Getting-rid-of-a-variable-conditionally/m-p/537637#M147893</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am trying to struggle to write a code in which I want to delete (get rid of) an entire variable if it contains only&amp;nbsp; "0" or "" . How can I write such a&amp;nbsp; code.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Data have;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;do i =1 to 5;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;if i=1 then do; a="1"; b="2",;c=""; end;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;if i=2 then do; a="3"; b="4";c="0"; end;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;if i=3 then do; a="5"; b="6"; c=""; end;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;if i=4 then do; a="7"; b="8"; c="0"; end;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;if i=5 then do; a="9"; b="10"; c=""; end;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;output;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;end;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Using this data , i want to get rid of c. Kindly help&lt;/P&gt;</description>
      <pubDate>Fri, 22 Feb 2019 13:33:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Getting-rid-of-a-variable-conditionally/m-p/537637#M147893</guid>
      <dc:creator>VinnyR</dc:creator>
      <dc:date>2019-02-22T13:33:18Z</dc:date>
    </item>
    <item>
      <title>Re: Getting rid of a variable, conditionally.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Getting-rid-of-a-variable-conditionally/m-p/537639#M147894</link>
      <description>You mean you want to drop the variable from the dataset itself if any one row in variable C has zero.</description>
      <pubDate>Fri, 22 Feb 2019 12:04:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Getting-rid-of-a-variable-conditionally/m-p/537639#M147894</guid>
      <dc:creator>Jagadishkatam</dc:creator>
      <dc:date>2019-02-22T12:04:50Z</dc:date>
    </item>
    <item>
      <title>Re: Getting rid of a variable, conditionally.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Getting-rid-of-a-variable-conditionally/m-p/537640#M147895</link>
      <description>&lt;P&gt;You can try something like the below code to drop the variable if a condition, i took the example of sashelp.class where sex='M' then i dropped the variables sex.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;please try and let me know if it helps.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data class;
set sashelp.class;
if sex='M' then do;
 drop sex;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 22 Feb 2019 12:09:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Getting-rid-of-a-variable-conditionally/m-p/537640#M147895</guid>
      <dc:creator>Jagadishkatam</dc:creator>
      <dc:date>2019-02-22T12:09:08Z</dc:date>
    </item>
    <item>
      <title>Re: Getting rid of a variable, conditionally.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Getting-rid-of-a-variable-conditionally/m-p/537641#M147896</link>
      <description>&lt;P&gt;Why do you have numbers stored as text?&amp;nbsp; This just makes your problem harder.&amp;nbsp; If they were numeric then you could do:&lt;/P&gt;
&lt;PRE&gt;data have (drop=i);
  do i =1 to 5;
     if i=1 then do; a=1; b=2; c=.; end;
     if i=2 then do; a=3; b=4; c=0; end;
     if i=3 then do; a=5; b=6; c=.; end;
     if i=4 then do; a=7; b=8; c=0; end;
     if i=5 then do; a=9; b=10; c=.; end;
  output;
  end;
run;

proc means data=have noprint;
  var _numeric_;
  output out=inter (drop=_type_ _freq_) sum=;
run;
proc transpose data=inter out=inter2;
  var _numeric_;
run;
proc sql noprint;
  select distinct _name_
  into  :drop_list separated by " "
  from  inter2
  where col1=0;
quit;
data want;
  set have (drop=&amp;amp;drop_list.);
run;&lt;/PRE&gt;
&lt;P&gt;Note, your have had errors.&lt;/P&gt;</description>
      <pubDate>Fri, 22 Feb 2019 12:12:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Getting-rid-of-a-variable-conditionally/m-p/537641#M147896</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2019-02-22T12:12:22Z</dc:date>
    </item>
    <item>
      <title>Re: Getting rid of a variable, conditionally.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Getting-rid-of-a-variable-conditionally/m-p/537647#M147900</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12151"&gt;@Jagadishkatam&lt;/a&gt;&amp;nbsp;drop is a "declarative" statement that the data step compiler acts upon when the data step is compiled; it can't be executed conditionally.&lt;/P&gt;
&lt;P&gt;Try this for verification:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data class;
set sashelp.class;
if sex='X' then do;
 drop sex;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 22 Feb 2019 12:43:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Getting-rid-of-a-variable-conditionally/m-p/537647#M147900</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-02-22T12:43:55Z</dc:date>
    </item>
    <item>
      <title>Re: Getting rid of a variable, conditionally.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Getting-rid-of-a-variable-conditionally/m-p/537651#M147901</link>
      <description>Thank you &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt; for the correction. I agree with you.</description>
      <pubDate>Fri, 22 Feb 2019 12:51:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Getting-rid-of-a-variable-conditionally/m-p/537651#M147901</guid>
      <dc:creator>Jagadishkatam</dc:creator>
      <dc:date>2019-02-22T12:51:21Z</dc:date>
    </item>
    <item>
      <title>Re: Getting rid of a variable, conditionally.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Getting-rid-of-a-variable-conditionally/m-p/537663#M147903</link>
      <description>Thanks RW9, i thought of this method as well. But it is too lengthy. I wanted something short and sweet. LIke one datastep. Is that possible?</description>
      <pubDate>Fri, 22 Feb 2019 13:32:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Getting-rid-of-a-variable-conditionally/m-p/537663#M147903</guid>
      <dc:creator>VinnyR</dc:creator>
      <dc:date>2019-02-22T13:32:40Z</dc:date>
    </item>
    <item>
      <title>Re: Getting rid of a variable, conditionally.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Getting-rid-of-a-variable-conditionally/m-p/537666#M147905</link>
      <description>&lt;P&gt;Nope, you need to go through the data and find indications of a certain value, then in another step drop those records.&amp;nbsp; You could do it using sql, but it wouldn't be any shorter.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The problem you have is that data is driving structure, which should not happen.&amp;nbsp; Structure should be fixed and used for programming.&amp;nbsp; Data should be be flexible and expandable, but always conform to structure.&amp;nbsp; So step 1 will be identify from the data, step 2 is to drop based on that, the two cannot be in one step.&lt;/P&gt;</description>
      <pubDate>Fri, 22 Feb 2019 13:36:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Getting-rid-of-a-variable-conditionally/m-p/537666#M147905</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2019-02-22T13:36:27Z</dc:date>
    </item>
    <item>
      <title>Re: Getting rid of a variable, conditionally.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Getting-rid-of-a-variable-conditionally/m-p/537736#M147936</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/158682"&gt;@VinnyR&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I totally agree with&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/45151"&gt;@RW9&lt;/a&gt;&amp;nbsp;, dynamic structures have no place in a production flow. But if you really want to get rid of all (unspecified) character variables in a data set with missing values, there is only the hard way. But you could put your program in a macro and store it in your permanent macro library. Here is a short macro to do it, is is intended as example only working on your test data and does not account for different libraries etc.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro cleands(dsin,dsout);
    %global vlist;
    proc sql noprint;
        select 'x'||name into :vlist separated by ' '
        from dictionary.columns
        where memname = upcase("&amp;amp;dsin") and type = 'char'
		order by varnum;
    quit;

	data _null_; set &amp;amp;dsin end=eof;
		array all $ _character_;
		retain &amp;amp;vlist;
		array vlist &amp;amp;vlist;
		do ix = 1 to dim(all);
			if all{ix} &amp;gt; '0' then vlist{ix} = 1;
		end;
		if eof then do;
			call execute("data &amp;amp;dsout;");
			call execute("set &amp;amp;dsin(drop=");
			do iy = 1 to dim(all);
				if vlist{iy} = '' then call execute( substr(vname(vlist{iy}),2));
			end;
			call execute(");");
			call execute("run;");
		end;
	run;
%mend;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then you don't even need a data step, just call the macro:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%cleands(have,want);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Nice and short, isn't it? - but it would still be better to use a keep list.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 24 Feb 2019 09:59:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Getting-rid-of-a-variable-conditionally/m-p/537736#M147936</guid>
      <dc:creator>ErikLund_Jensen</dc:creator>
      <dc:date>2019-02-24T09:59:40Z</dc:date>
    </item>
    <item>
      <title>Re: Getting rid of a variable, conditionally.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Getting-rid-of-a-variable-conditionally/m-p/537740#M147939</link>
      <description>I forgot to delete the put statement in first do loop before posting...</description>
      <pubDate>Fri, 22 Feb 2019 16:11:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Getting-rid-of-a-variable-conditionally/m-p/537740#M147939</guid>
      <dc:creator>ErikLund_Jensen</dc:creator>
      <dc:date>2019-02-22T16:11:26Z</dc:date>
    </item>
    <item>
      <title>Re: Getting rid of a variable, conditionally.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Getting-rid-of-a-variable-conditionally/m-p/537957#M148044</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; 

Data have;
   do i =1 to 5;
   if i=1 then do; a="1"; b="2";c=""; end;
   if i=2 then do; a="3"; b="4";c="0"; end;
   if i=3 then do; a="5"; b="6"; c=""; end;
   if i=4 then do; a="7"; b="8"; c="0"; end;
   if i=5 then do; a="9"; b="10"; c=""; end;
   output;
   end;
run;
proc sql;
create table temp as
 select sum(a not in (' ' '0')) as a,
 sum(b not in (' ' '0')) as b,
 sum(c not in (' ' '0')) as c
 from have;
quit;
proc transpose data=temp out=temp1;
run;
proc sql;
select _name_ into : list separated by ',' from temp1
 where col1=0;

alter table have
drop &amp;amp;list;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 23 Feb 2019 11:59:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Getting-rid-of-a-variable-conditionally/m-p/537957#M148044</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2019-02-23T11:59:07Z</dc:date>
    </item>
    <item>
      <title>Re: Getting rid of a variable, conditionally.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Getting-rid-of-a-variable-conditionally/m-p/537958#M148045</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; 

Data have;
   do i =1 to 5;
   if i=1 then do; a="1"; b="2";c=""; end;
   if i=2 then do; a="3"; b="4";c="0"; end;
   if i=3 then do; a="5"; b="6"; c=""; end;
   if i=4 then do; a="7"; b="8"; c="0"; end;
   if i=5 then do; a="9"; b="10"; c=""; end;
   output;
   end;
run;
proc sql;
create table temp as
 select sum(a not in (' ' '0')) as a,
 sum(b not in (' ' '0')) as b,
 sum(c not in (' ' '0')) as c
 from have;
quit;
proc transpose data=temp out=temp1;
run;
proc sql;
select _name_ into : list separated by ',' from temp1
 where col1=0;

alter table have
drop &amp;amp;list;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 23 Feb 2019 11:59:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Getting-rid-of-a-variable-conditionally/m-p/537958#M148045</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2019-02-23T11:59:29Z</dc:date>
    </item>
    <item>
      <title>Re: Getting rid of a variable, conditionally.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Getting-rid-of-a-variable-conditionally/m-p/538002#M148067</link>
      <description>&lt;P&gt;I find it a daring business to make in situ changes to data. I always try to discourage people from doing that, because it is irreversible. It might be difficult to get a fresh copy if the code fails and input data are lost, and even if it works it is impossible to prove that it worked as intended. So I think it should be one of the "best practice" rules to keep original data for documentational purposes.&lt;/P&gt;</description>
      <pubDate>Sat, 23 Feb 2019 18:19:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Getting-rid-of-a-variable-conditionally/m-p/538002#M148067</guid>
      <dc:creator>ErikLund_Jensen</dc:creator>
      <dc:date>2019-02-23T18:19:30Z</dc:date>
    </item>
    <item>
      <title>Re: Getting rid of a variable, conditionally.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Getting-rid-of-a-variable-conditionally/m-p/538018#M148074</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12887"&gt;@ErikLund_Jensen&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;I forgot to delete the put statement in first do loop before posting...&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Then why not just edit the posting and fix it?&lt;/P&gt;</description>
      <pubDate>Sat, 23 Feb 2019 21:26:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Getting-rid-of-a-variable-conditionally/m-p/538018#M148074</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-02-23T21:26:45Z</dc:date>
    </item>
    <item>
      <title>Re: Getting rid of a variable, conditionally.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Getting-rid-of-a-variable-conditionally/m-p/538063#M148087</link>
      <description>Sorry, I never considered that a possibility. It is done now, but I didn't find a way to delete my correction post, that would be nice too.&lt;BR /&gt;</description>
      <pubDate>Sun, 24 Feb 2019 10:04:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Getting-rid-of-a-variable-conditionally/m-p/538063#M148087</guid>
      <dc:creator>ErikLund_Jensen</dc:creator>
      <dc:date>2019-02-24T10:04:47Z</dc:date>
    </item>
    <item>
      <title>Re: Getting rid of a variable, conditionally.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Getting-rid-of-a-variable-conditionally/m-p/538207#M148146</link>
      <description>&lt;P&gt;Please find the code.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data have;
   do i =1 to 5;
   if i=1 then do; a="1"; b="2"; c=""; end;
   if i=2 then do; a="3"; b="4"; c="0"; end;
   if i=3 then do; a="5"; b="6"; c=""; end;
   if i=4 then do; a="7"; b="8"; c="0"; end;
   if i=5 then do; a="9"; b="10"; c=""; end;
   output;
   end;
run;

%macro drop_empty_var(ds_in=,ds_out=);
	data _null_;
	set &amp;amp;ds_in.;
	array my_nums[*] _numeric_;
	array my_chars[*] _character_;
	call symput ('dim_n',dim(my_nums));
	call symput ('dim_c',dim(my_chars));
	stop;
	run;

	data _null_;
	set &amp;amp;ds_in. end=last;
	length drop_numvars $2000 drop_charvars $2000;
	array num_delete[&amp;amp;dim_n.];
	array char_delete[&amp;amp;dim_c.];
	array my_nums[*] _numeric_;
	array my_chars[*] _character_;
	retain num_delete char_delete;
	do j=1 to &amp;amp;dim_n.;
		if _n_=1 or num_delete[j] =0 then do;
			/*Condition to delete numeric Variable*/
			if my_nums[j]=. or my_nums[j]=0 then num_delete[j] =0;
			else num_delete[j] =1;
		end; 
	end;
	do j=1 to &amp;amp;dim_c.;
		if _n_=1 or char_delete[j] =0 then do;
			/*Condition to delete charecter Variable*/
			if strip(my_chars[j])='.' or strip(my_chars[j])='' or strip(my_chars[j])='0' then char_delete[j] =0;
			else char_delete[j] =1;
		end; 
	end;

	if last then do;
		do j=1 to &amp;amp;dim_n.;
			if num_delete[j]=0 then do;
				drop_numvars=cats(drop_numvars,',',vname(my_nums[j]));
			end;
		end;
		do j=1 to &amp;amp;dim_c.;
			if char_delete[j]=0 then do;
				drop_charvars=cats(drop_charvars,',',vname(my_chars[j]));
			end;
		end;
		call symput('drop_numvars',translate(drop_numvars,' ',','));
		call symput('drop_charvars',translate(drop_charvars,' ',','));
	end;
	run;

	data &amp;amp;ds_out.(drop=&amp;amp;drop_numvars. &amp;amp;drop_charvars.);
	set &amp;amp;ds_in.;
	run;
%mend drop_empty_var;
%drop_empty_var(ds_in=have,ds_out=want);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;It has one additional data step before dropping the variables.&lt;/P&gt;&lt;P&gt;Please let us know it it worked for you.&lt;/P&gt;</description>
      <pubDate>Mon, 25 Feb 2019 08:06:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Getting-rid-of-a-variable-conditionally/m-p/538207#M148146</guid>
      <dc:creator>Satish_Parida</dc:creator>
      <dc:date>2019-02-25T08:06:06Z</dc:date>
    </item>
  </channel>
</rss>

