Help using Base SAS procedures

Check existence of the variable...

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 145
Accepted Solution

Check existence of the variable...

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;


Accepted Solutions
Solution
‎10-28-2014 11:13 AM
Super User
Super User
Posts: 7,970

Re: Check existence of the variable...

Posted in reply to rakeshvvv

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


All Replies
Solution
‎10-28-2014 11:13 AM
Super User
Super User
Posts: 7,970

Re: Check existence of the variable...

Posted in reply to rakeshvvv

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.

Frequent Contributor
Posts: 144

Re: Check existence of the variable...

Posted in reply to rakeshvvv

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;

Regular Contributor
Posts: 180

Re: Check existence of the variable...

Posted in reply to arodriguez

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

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

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