BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ayin
Quartz | Level 8

Have:

data sample;
	input make $ mpg rep78;
cards;
AMC 22 3
;
run;

data sample2;
set sample;
label	rep78	= "Please drop me"
		mpg	= "Keep me in the dataset"
		make	= "ignore me";
run;

data reference;
	input label_dropped $char32.;
cards;
Please drop me
ignore me
;
run;

Want:

drop columns 'rep78' and 'make' in dataset 'sample2', because their labels are listed in the dataset 'reference' (column 'label_dropped').

 

How to achieve this?

 

Thanks.

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
ayin
Quartz | Level 8
%global droplist;

%macro test;
proc sql;
create table table as
select name, label from sashelp.vcolumn
	where libname = 'WORK' and memname = 'SAMPLE2';

create table table2 as
	select * from table a
	right join reference b
	on b.label_dropped = a.label;

select name into :droplist separated by ' '
from table2;

quit;
%mend test;
%test;

data sample_final;
set sample;

drop &droplist;
run;

Codes are not perfect.

View solution in original post

4 REPLIES 4
Reeza
Super User

Use SASHELP.VCOLUMN to retrieve the variables with same labels and then add them to a DROP statement. 

Ideally you can do the drop via PROC DATASETS or SQL so you don't have to recreate the entire table. I'm not sure that possible though. 

 

 

ayin
Quartz | Level 8
%global droplist;

%macro test;
proc sql;
create table table as
select name, label from sashelp.vcolumn
	where libname = 'WORK' and memname = 'SAMPLE2';

create table table2 as
	select * from table a
	right join reference b
	on b.label_dropped = a.label;

select name into :droplist separated by ' '
from table2;

quit;
%mend test;
%test;

data sample_final;
set sample;

drop &droplist;
run;

Codes are not perfect.

Reeza
Super User

Nice work. 

You can combine the SQL steps, just make sure that you account for case differences as well. This is untested but hopefully gives you the idea of what to do

 

proc sql;
create table table as
select name into : droplist separated by " " 
from sashelp.vcolumn
	where libname = 'WORK' and memname = 'SAMPLE2' and upper(label) in (select upper(label_dropped) from reference);
quit;
Reeza
Super User

edit:

 

proc sql noprint;
select name into : droplist separated by " " 
from sashelp.vcolumn
	where libname = 'WORK' and memname = 'SAMPLE2' and upper(label) in (select upper(label_dropped) from reference);
quit;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 2247 views
  • 3 likes
  • 2 in conversation