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
SAS Super FREQ
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!

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!

New Learning Events in April

 

Join us for two new fee-based courses: Administrative Healthcare Data and SAS via Live Web Monday-Thursday, April 24-27 from 1:00 to 4:30 PM ET each day. And Administrative Healthcare Data and SAS: Hands-On Programming Workshop via Live Web on Friday, April 28 from 9:00 AM to 5:00 PM ET.

LEARN MORE

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