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

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
1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

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;


--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

View solution in original post

3 REPLIES 3
Astounding
PROC Star

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;

ballardw
Super User

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;
mkeintz
PROC Star

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;


--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 17248 views
  • 5 likes
  • 4 in conversation