Help using Base SAS procedures

How to keep variables end(not begin ) with same characters

Reply
Contributor
Posts: 35

How to keep variables end(not begin ) with same characters

data one;

input a b c a_123 b_123 x_123 y_123  z_123;

run;

data two;

set one;

keep a_123 b_123 x_123 y_123  z_123;

run;

My question is if there are too much variables end with '_123'(I can't list all of them here),how to keep these variables in an easy way?

I know ':' operator can keep variables start with special characters but not end with.

Thanks!

Super Contributor
Posts: 1,636

Re: How to keep variables end(not begin ) with same characters

How about:

(note: libname and memname must be capital letters)

proc sql noprint;

   select name into : names separated by ' '

   from dictionary.columns

   where libname='WORK' and memname='HAVE' and upcase(name) like '%_123' ;

quit;

data want;

  set have(keep=&names);

run;

Linlin

Super Contributor
Posts: 276

How to keep variables end(not begin ) with same characters

Just curious....

How to do this  in Data Step???

Respected Advisor
Posts: 3,156

How to keep variables end(not begin ) with same characters

Posted in reply to kuridisanjeev

You could have more than one data step approaches. For one, you could use meta view for data step, such as sashelp.vcolumn, and use a combination of substr() and length to obtain the variable names. For two, you could use array vname to grab the variable names that meets criteria.

You could also use proc contents instead of SQL to get the variables names.

However, in case, by supporting operators such as 'like', 'contains', SQL is more native to the job.

Haikuo

Ask a Question
Discussion stats
  • 3 replies
  • 3392 views
  • 3 likes
  • 4 in conversation