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

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
  • 694 views
  • 3 likes
  • 3 in conversation