BookmarkSubscribeRSS Feed
jkim197
Obsidian | Level 7

Hello all.

 

I have a simple question for base sas programming.

I want to delete variables if their name meet some condition.

 

For example

I have variables port1_1, port1_2, port1_3, port2_1, port2_2, port2_3, port3_1, port3_2, port3_3

I want to delete port1_1, port2_2, port3_3

 

I made a list of variable name to delete in separate dataset, but I don't know how to use them...

 

 

What I have is two datasets

 

have1 and have2

 

<have1>

familystockport1_1port1_2port1_3port1_4port2_1port2_2port2_3port2_4port3_1port3_2port3_3port3_4port4_1port4_2port4_3port4_4
11101.000.101.....
12110.110.000.....
13101.000.101.....
14111.111.111.....
15000.011.011.....
16111.111.111.....
17000.010.000.....
19000.010.000.....
111100.000.000.....
112111.111.111.....
113111.111.111.....
115110.110.000.....
116000.010.000.....
117110.110.000.....
118110.110.000.....
119000.010.000.....
121110.110.000.....
123100.000.000.....
124100.000.000.....
211111111111111111
221101110100001101
231101110100001101
241101110100001101
251101110100001101
261001000000001001
271110111011100000
281000000000000000
290000011001100000
2110000011001100000
2120000000000100000
2270000000000110011
2280000000000110011
2290000000000110011

 

 

<have2> --> they are the variables that I want to delete

Variable Name
port1_1
port2_2
port3_3
port4_4

 

how do I efficiently delete them??

 

2 REPLIES 2
Kurt_Bremser
Super User

Use proc sql to create a list for dropping:

proc sql noprint;
select name into :deletelist separated by ' ' from have2;
quit;

data want;
set have;
drop &deletelist;
run;

With suitable conditions, you can pull such lists directly from dictionary.columns.

Astounding
PROC Star

@Kurt_Bremser has the right idea.  In part, the answer depends on what you mean by "efficient".  To get his answer to run  a little bit faster, change the final step to:

 

data want;
set have (drop=&deletelist);
run;

 

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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