I Have a library with around 20 datasets. I want to write a macro in such way that it should check for the existence of the variable and If that variable present. Would need to do further processing. I wrote upto this part…….
PROC SQL;
CREATE TABLE TABLIST AS SELECT MEMNAME FROM DICTIONARY.TABLES
WHERE LIBNAME = "xxx" ;
QUIT;
%MACRO LOOP;
%LET DSID=%SYSFUNC(OPEN(TABLIST,I));
%LET NOBS=%SYSFUNC(ATTRN(&DSID,NOBS));
%LET RC=%SYSFUNC(CLOSE(&DSID));
%DO J=1 %TO &NOBS;
DATA _NULL_;
SET TABLIST;
IF _N_=&J THEN DO;
CALL SYMPUT ('DOM', COMPRESS(UPCASE(MEMNAME)));
END;
RUN;
Have to determine which one of these variable exists in the table....
&dom.term,&dom.testcd,&dom.trt.....
any one of the above variable will be present...based on the existence of which variable the below code continues as
DATA &DOM;
SET xxx.&DOM;
WHERE not missing (&dom.term)(&dom.testcd)(&dom.trt);
RUN;
%mend loop;
Well, from my side I would do:
data _null_;
set sashelp.vcolumn (where=(libname="SASHELP" and memname="CARS" and name="MAKE")); /* You can modify for your variables/tables */
call execute(' .. '); /* do additional programming, could be a macro call or actual code */
run;
So if the column(s) specified exist in the dataset(s) supplied, then the call execute generates the code per record. If it doesn't exist then nothing happens. Avoids the whole macro thing.
Well, from my side I would do:
data _null_;
set sashelp.vcolumn (where=(libname="SASHELP" and memname="CARS" and name="MAKE")); /* You can modify for your variables/tables */
call execute(' .. '); /* do additional programming, could be a macro call or actual code */
run;
So if the column(s) specified exist in the dataset(s) supplied, then the call execute generates the code per record. If it doesn't exist then nothing happens. Avoids the whole macro thing.
When I want to know if a variable exist I use this code
data _null_;
set ds;
dsid = open("ds");
if varnum(dsid,"var") eq 0 then call symputx('exist',0); /*if not exist, changed to 0*/
else call symputx('exist',1);
run;
I agree with RW9.
You can also find the variable names in any table using DICTIONARY TABLES. You do not need to open the dataset in advance.
CTorres
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.