DATA Step, Macro, Functions and more

keep variables when their names meet some condition

Reply
Contributor
Posts: 32

keep variables when their names meet some condition

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??

 

Super User
Posts: 10,574

Re: keep variables when their names meet some condition

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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super User
Posts: 6,934

Re: keep variables when their names meet some condition

Posted in reply to KurtBremser

@KurtBremser 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;

 

Ask a Question
Discussion stats
  • 2 replies
  • 111 views
  • 3 likes
  • 3 in conversation