BookmarkSubscribeRSS Feed
VanDalucas
Obsidian | Level 7

Hi,

 

I have a big data set with more than 4000 columns and need to keep columns that have specific sequence of characters in their name (e.g. "_US_"). Can I that using wildcards in "Keep" statement?

 

thank you

5 REPLIES 5
arodriguez
Lapis Lazuli | Level 10
If they start with the same sequence of characters you could say something like _US_:.
If they are all together in the dataset you could say "all variables between X and Y" with X--Y.
Loko
Barite | Level 11

Hello,

 

If the sequence is in the middle of the character name you can select those variable in a macro statement separated by space and

use this macro variable in a keep statement.

 

For doing this operation you shall use the dictionary table - vcolumn:

 

proc sql;
select name into :vr separated by ' '
from sashelp.vcolumn
where libname eq 'SASHELP' and memname eq 'CLASS'
and name like '%h%';
quit;

data want;
set sashelp.class;
keep &vr;
run;
stat_sas
Ammonite | Level 13

Hi,

 

On a similar pattern as per @Loko suggestion:

 

proc contents data=have out=vars(keep=name);
run;

 

proc sql;
select name into :vars_needed separated by ' '
from vars
where find(name,"_US_")>0;
quit;

 

%put &vars_needed;

VanDalucas
Obsidian | Level 7

I really thank all of you for you replies,

The solution using proc sql worked fine,

 

(I was just wondering if a more ''complex'' KEEP statement could do the job but it seems there is no such possibility)

 

 

LinusH
Tourmaline | Level 20
Having 4000 columns is usually not a very user/programming friendly structure. You'll probably encounter other similar problems down the road.
Consider transposing your data to long format, then you'll be able to do more clever things with simpler filtering and grouping.
Data never sleeps

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 5 replies
  • 1162 views
  • 7 likes
  • 5 in conversation