BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
rakeshvvv
Quartz | Level 8

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;

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

View solution in original post

3 REPLIES 3
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

arodriguez
Lapis Lazuli | Level 10

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;

CTorres
Quartz | Level 8

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: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

What is Bayesian Analysis?

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 4616 views
  • 0 likes
  • 4 in conversation