BookmarkSubscribeRSS Feed
deleted_user
Not applicable
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.
3 REPLIES 3
Cynthia_sas
Diamond | Level 26
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]
Olivier
Pyrite | Level 9
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
deleted_user
Not applicable
Thanks a lot! I'm very grateful!!

Best regards!

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

Health and Life Sciences Learning

 

Need courses to help you with SAS Life Sciences Analytics Framework, SAS Health Cohort Builder, or other topics? Check out the Health and Life Sciences learning path for all of the offerings.

LEARN MORE

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