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;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 4 replies
  • 1442 views
  • 3 likes
  • 2 in conversation