BookmarkSubscribeRSS Feed
glcoolj12
Obsidian | Level 7

Hello,

 

I've created a very basic macro to bring in several datasets.  I only want to keep variables in the datasets that contain the word "PID" (it's typically the last 3 characters in the variable name but that's not always the case).  I created the following macro, but I get an error saying that "variable VARNAME is not on file".   Any help would be greatly appreciated, thanks!

%macro datain (old,new);
data &new;
set dataset.&old;
where VARNAME contains 'PID';
run;
%mend;
5 REPLIES 5
RW9
Diamond | Level 26 RW9
Diamond | Level 26

There is no such variable called varname in your dataset, where did you get the idea there was?  How are you getting the data, as if you used a good methodology - text file and write datastep - then you would just omit these from the datastep import process. 

proc sql;
  select distinct NAME
  into   :L separated by " "
  from   SASHELP.VCOLUMN
  where  LIBNAME="WORK" 
     and MEMNAME="HAVE"
     and index(NAME,"PID") > 0;
quit;

data want;
  set have (drop=&L.);
run;
art297
Opal | Level 21

Slight change from the code suggested by @RW9 as that dropped rather than kept the variable(s) of interest, and the following keeps it as a macro:

%macro datain (old,new);
  proc sql noprint;
    select name into :keeps separated by ' '
      from dictionary.columns
        where libname eq 'DATASET' and
              memname eq upcase("&old.") and
              upcase(name) contains 'EX'
    ;
  quit;
  
  data &new;
    set dataset.&old (keep=&keeps.);
  run;
%mend datain;

libname dataset '/folders/myfolders';
data dataset.test;
  set sashelp.class;
run;

%datain(test,newtest)

Art, CEO, AnalystFinder.com

Astounding
PROC Star

If your data set were to contain these variables, would you want to keep them?

 

CUPID

STUPID

PIDDLE

RAPID

glcoolj12
Obsidian | Level 7

Yes.  

 

'dataset' is the libname I assigned to the folder that holds all 50+ datasets (.sas7bdat)

glcoolj12
Obsidian | Level 7

Thank you all for your help.  I was able to piece together a program that works.  Thanks!! 

%macro k;
%put &keep_vars.;
%mend k;

%macro datain (old, new); data &new; set dataset.&old; run; proc sql noprint; select trim(compress(name)) into :keep_vars separated by ' ' from sashelp.vcolumn where libname = upcase('work') and memname = upcase('&new.') and upcase(name) like '%PID%'; quit; %k; data &new; set &new; keep &keep_vars.; run; %mend;

 

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
  • 5 replies
  • 917 views
  • 3 likes
  • 4 in conversation