What you are attempting is not going to be a one line of code operation though after you get something working without macro code then something can be done.
Steps would be
1) verify the existence of the dataset
2) verify the existence of the variable in that data set
if both of those conditions are met then you can start looking for specific values.
For instance:
proc sql noprint;
select *
from dictionary.columns
where Libname='SASHELP' and Memname=('CLASS')
and upcase(name)='SEX'
;
quit;
%put &sqlobs;
The select examines you SAS metadata which has libraries (LIBNAME), data sets (MEMNAME) and variables (NAME) plus characteristics of the variables. The macro variable SQLOBS is an automatic variable that contains the number of records returned by the latest Proc SQL query. &sqlobs will be 0 if no records were returned. Note that the Libname and Memname are stored as upper case in the dictionary table, variablenames may be mixed case so be careful on syntax using those.
So you could run that SQL snippet and and then use
%if &sqlobs > 0 %then %do.
If I were doing this frequently might create function or small macro to return the value of sqlobs when passed a library (which could default to WORK) , a data set name and a variable name as parameters. I'll leave that as a small exercise for the intersted reader at this time.
... View more