DATA Step, Macro, Functions and more

How to select variables having a particular string as part of its name

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 96
Accepted Solution

How to select variables having a particular string as part of its name

Hi there,

For your kind information, I am trying to select a group of variables having a particular string as part of its name. For e.g. from my dataset "HAVE", I want only those variable which are having " Completeness" as part of its name. 

data have ;
input aa_completeness  bb_consistency bb_completeness cc_accuracy cc_completeness dd_consistency;
datalines ;
90 80 70 60 50 40
; 
run;


data want ;
input aa_completeness  bb_completeness cc_completeness ;
datalines ;
90 70 50
; 
run;

Thank you in advance for your kind reply to move forward. 

Regards,

 

Swain

Accepted Solutions
Solution
‎05-17-2017 07:45 PM
Valued Guide
Posts: 797

Re: How to select variables having a particular string as part of its name

A pure data step solution:

 

filename tmp temp;

data _null_;
  file tmp;
  if 0 then set have;
  length varname $32;
  do until (varname='varname');
    call vnext(varname);
    if index(varname,'_completeness') then put varname;
  end;
run;

data want;
   set have;
   keep
     %include tmp;
   ;
run;


View solution in original post


All Replies
Super User
Posts: 5,085

Re: How to select variables having a particular string as part of its name

[ Edited ]

SAS gives you a couple of ways to capture variable names.  Here's one:

 

proc contents data=have noprint out=_contents_ (keep=name);

run;

 

proc sql;

select strip(name) into : compl_list separated by ' '

from _contents_

where index(upcase(name), 'COMPLETENESS') > 0;

quit;

 

This should give you a macro variable &COMPL_LIST that holds the names you are looking for.  Use as you need to, such as:

 

data want;

set have (keep=&COMPL_LIST);

run;

Super User
Posts: 10,516

Re: How to select variables having a particular string as part of its name

Something like the following should work. Note that in the Dictionary table that the variable Libname and Memname (the data set) are in upper case but Name (the variable name) may be mixed. So you want to consider case in comparisons. Any you really want to specify the library as if you have multiple libraries it will find the variables from all data sets with the same name.

 

proc sql noprint;
   select name into : varnames separated by " "
   from dictionary.columns
   where libname="WORK" and memname="HAVE"
     and upcase(name) contains "COMPLETENESS";
quit;

data want;
   set have (keep = &varnames);
run;
Solution
‎05-17-2017 07:45 PM
Valued Guide
Posts: 797

Re: How to select variables having a particular string as part of its name

A pure data step solution:

 

filename tmp temp;

data _null_;
  file tmp;
  if 0 then set have;
  length varname $32;
  do until (varname='varname');
    call vnext(varname);
    if index(varname,'_completeness') then put varname;
  end;
run;

data want;
   set have;
   keep
     %include tmp;
   ;
run;


☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 129 views
  • 3 likes
  • 4 in conversation