BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
rykwong
Quartz | Level 8

Dear SAS community:

I have a large dataset with several thousand variables. 

I need to pick variables using specific texts in their variable names. 

I use this simple code to pick them:

 

data two; set one;

keep mi:;

run;

 

this picks up the variables that starts with the name "mi", such as mi_date, mi_size, and mi_treat, but cannot pick up hx_mi, time_mi, and chronic_mi_treat.  is there something simple I can use to pick out variables using "mi" in the variable names regardless of position?

 

thanks

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

Something simple?  No.  But the complexities are manageable.  Old-style program:

 

proc contents data=have out=_contents_ (keep=name where=(index(upcase(name), 'MI') > 0));

run;

 

That gives you a list of all the variable names.  Note that you can use dictionary.columns instead of PROC CONTENTS if that's something y ou are mildly familiar with.

 

Then get the list into a macro variable (again you have choices at this point, CALL EXECUTE being one alternative):

 

proc sql;

select strip(name) into : varlist separated by ' ' from _contents_;

quit;

 

Finally, use the macro variable:

 

data want;

set have (keep=&varlist);

run;

 

Note that you can add to the list of variables, such as:

 

set have (keep=id &varlist);

 

Finally, consider whether it would be safer (but still accurate) to search for "mi_" instead of "mi".

View solution in original post

2 REPLIES 2
Astounding
PROC Star

Something simple?  No.  But the complexities are manageable.  Old-style program:

 

proc contents data=have out=_contents_ (keep=name where=(index(upcase(name), 'MI') > 0));

run;

 

That gives you a list of all the variable names.  Note that you can use dictionary.columns instead of PROC CONTENTS if that's something y ou are mildly familiar with.

 

Then get the list into a macro variable (again you have choices at this point, CALL EXECUTE being one alternative):

 

proc sql;

select strip(name) into : varlist separated by ' ' from _contents_;

quit;

 

Finally, use the macro variable:

 

data want;

set have (keep=&varlist);

run;

 

Note that you can add to the list of variables, such as:

 

set have (keep=id &varlist);

 

Finally, consider whether it would be safer (but still accurate) to search for "mi_" instead of "mi".

rykwong
Quartz | Level 8

many thanks Astounding,

fantastic, this is simple enough

thanks

 

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
  • 1140 views
  • 1 like
  • 2 in conversation