DATA Step, Macro, Functions and more

Macro for Keeping variable names containing specific string

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 18
Accepted Solution

Macro for Keeping variable names containing specific string

I have several datasets (.sas7bdat files in libname 'dataset') that I'm trying to read into my program.  I only want to keep variables that contain 'ID' in the name. I have code that does what I want it to do, but I want to put it in a macro.

 

When I run the code in the macro I created, I have an issue at the PROC SQL/%PUT part of my code and I get warning that says, "No rows selected.......no KEEP variables found, statement ignored."  Thus, it reads in the datasets in but doesn't keep the variables I want.  

 

Any suggestions?  Thank you. 

%macro datain(old,new,final);
   data &new;
   set dataset.&old;
   run;

proc sql noprint;
select trim(compress(name))
into :keep_vars separated by ' '
from dictionary.columns
where libname = upcase('work')
and memname = upcase('&new.')
and upcase(name) like '%ID%';
quit;

%put &keep_vars.;

data &final;
set &new;
keep &keep_vars.;
run;
%mend datain;

Accepted Solutions
Solution
‎07-31-2017 03:07 PM
Super User
Posts: 5,505

Re: Macro for Keeping variable names containing specific string

Posted in reply to glcoolj12

Single quotes prevent resolution of macro variables:  ('&new.')

 

Use double quotes instead:  ("&new.")

View solution in original post


All Replies
Trusted Advisor
Posts: 1,918

Re: Macro for Keeping variable names containing specific string

Posted in reply to glcoolj12

glcoolj12 wrote:

I have several datasets (.sas7bdat files in libname 'dataset') that I'm trying to read into my program.  I only want to keep variables that contain 'ID' in the name. I have code that does what I want it to do, but I want to put it in a macro.

 

When I run the code in the macro I created, I have an issue at the PROC SQL/%PUT part of my code and I get warning that says, "No rows selected.......no KEEP variables found, statement ignored."  Thus, it reads in the datasets in but doesn't keep the variables I want.  

 

Any suggestions?  Thank you. 


Really, we don't know what variables you have. This could be a completely correct result. We don't know. And we can't debug this ... unless ... you show us that you do indeed have variables with ID in the name, perhaps by showing us a PROC CONTENTS or the appropriate parts of sashelp.vcolumn.

Solution
‎07-31-2017 03:07 PM
Super User
Posts: 5,505

Re: Macro for Keeping variable names containing specific string

Posted in reply to glcoolj12

Single quotes prevent resolution of macro variables:  ('&new.')

 

Use double quotes instead:  ("&new.")

Occasional Contributor
Posts: 18

Re: Macro for Keeping variable names containing specific string

Posted in reply to Astounding
This fixed the issue - thank you so much!
☑ This topic is solved.

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

Discussion stats
  • 3 replies
  • 174 views
  • 0 likes
  • 3 in conversation