BookmarkSubscribeRSS Feed
Satish_Parida
Lapis Lazuli | Level 10

Please find the code.

 

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 &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 &ds_in. end=last;
	length drop_numvars $2000 drop_charvars $2000;
	array num_delete[&dim_n.];
	array char_delete[&dim_c.];
	array my_nums[*] _numeric_;
	array my_chars[*] _character_;
	retain num_delete char_delete;
	do j=1 to &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 &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 &dim_n.;
			if num_delete[j]=0 then do;
				drop_numvars=cats(drop_numvars,',',vname(my_nums[j]));
			end;
		end;
		do j=1 to &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 &ds_out.(drop=&drop_numvars. &drop_charvars.);
	set &ds_in.;
	run;
%mend drop_empty_var;
%drop_empty_var(ds_in=have,ds_out=want);

It has one additional data step before dropping the variables.

Please let us know it it worked for you.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 15 replies
  • 1223 views
  • 2 likes
  • 8 in conversation