Look for a variable!

Reply
N/A
Posts: 0

Look for a variable!

Hi there!

Somebody knows how to know if an especific variable exists in a dataset! I need this for a sas macro program, so I would need something like a function that tell me yes or no if the variable exists!

Thanks!! Best regards.

Juan Vicente.
SAS Super FREQ
Posts: 8,719

Re: Look for a variable!

Hi!
Here's a SAS macro program that does the lookup for you. It uses a special dataset, SASHELP.VCOLUMN -- one of the "dictionary" datasets that list things like table names, library names, columns, etc. There may be an SCL function, but if so, I'm not conversant enough with SCL to find it.
When you invoke the macro program the first time
[pre]
%lookvar(lib=sashelp, mem=prdsale, var=region);
[/pre]
the macro program CREATES a macro variable called GOTVAR that will be set to 'yes' if the desired variable was found. Then it makes the library and memname and var name global variables so they can be accessed by the %USEINFO macro program.
It doesn't exactly invoke like a function, but close.
cynthia
[pre]
options nomprint nosymbolgen nomlogic;
%macro lookvar(lib=sashelp, mem=class, var=age);
%global gotvar slib smem svar;
%let gotvar=no;
%let lib = %upcase(&lib);
%let mem = %upcase(&mem);
%let var = %upcase(&var);
%let svar = %upcase(&var);
%let slib = %upcase(&lib);
%let smem = %upcase(&mem);

data findvar(keep=libname memname name type length format label);
set sashelp.vcolumn;
where libname="&lib" and memname="&mem";
if upcase(name) = "&var" then do;
call symput('gotvar','yes');
end;
run;

%mend lookvar;

%macro useinfo;
%global gotvar;
%if &gotvar = yes %then %do;
proc print data=&slib..&smem (obs=10);
title "The variable &svar found in &slib..&smem";
run;
%end;
%if &gotvar = no %then %do;
%put =====> =====> <===== <=====;
%put Variable &svar NOT found in &slib..&smem;
%put =====> =====> <===== <=====;
%end;

%mend useinfo;

%lookvar(lib=sashelp, mem=prdsale, var=region);
run;
%useinfo;
run;

** after LOOKVAR executes, you can test the value;
** of GOTVAR since it is a global variable;

%lookvar(lib=sashelp, mem=prdsale, var=age);
run;
%useinfo;
run;
[/pre]
Super Contributor
Posts: 260

Re: Look for a variable!

If you want macro-functions or something similar, you should consider using the OPEN and VARNUM functions. The first one lets you exploring a dataset without starting any procedure nor data step, and the second gives the number (1 for the variable at the left of your dataset) of the variable, and 0 if it is not in the dataset.
For macro-using, outside of any data step, combine the functions with the %SYSFUNC macro-function.

%PUT %SYSFUNC(VARNUM(%SYSFUNC(OPEN(sashelp.class)),age)) ;
--> 3 as AGE is the third variable in SASHELP.CLASS

%PUT %SYSFUNC(VARNUM(%SYSFUNC(OPEN(sashelp.class)),x)) ;
--> 0 as X is not a variable from SASHELP.CLASS

Regards
N/A
Posts: 0

Re: Look for a variable!

Thanks a lot! I'm very grateful!!

Best regards!
Ask a Question
Discussion stats
  • 3 replies
  • 377 views
  • 0 likes
  • 3 in conversation