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-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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