okay, so i built this macro a while ago. it was in a different thread in this forum. i used it in the past and it worked however i am getting weird, kind of unidentified errors...
%macro VarExist(ds,var);
%local rc dsid result resx;
%let dsid = %sysfunc(open(&ds));
%let resx = %sysfunc(varnum(&dsid,&var));
%let rc = %sysfunc(close(&dsid));
%if &resx > 0 %then %do;
%let result = 1;
%put NOTE: Var &var exists in &ds;
%end;
%else %do;
%let result = 0;
%put NOTE: Var &var not exists in &ds;
data &dsid;
&var='UNK';
set &dsid;
run;
%end;
%mend VarExist;
%VarExist(TABLE_01,FIELD_01);
however, like i said, when i run this i get this error...
93
94
98
99 %macro VarExist(ds,var);
101 %local rc dsid result resx;
102 %let dsid = %sysfunc(open(&ds));
103 %let resx = %sysfunc(varnum(&dsid,&var));
104 %let rc = %sysfunc(close(&dsid));
105
106 %if &resx > 0 %then %do;
107 %let result = 1;
108 %put NOTE: Var &var exists in &ds;
109
110 %end;
111
112 %else %do;
113 %let result = 0;
114 %put NOTE: Var &var not exists in &ds;
115
116 data &dsid;
117 set &dsid;
118 &var='UNK';
119 run;
120
121 %end;
122 %mend VarExist;
123
124 %VarExist(TABLE_01,FIELD_01);
MLOGIC(VAREXIST): Beginning execution.
MLOGIC(VAREXIST): Parameter DS has value TABLE_01
MLOGIC(VAREXIST): Parameter VAR has value FIELD_01
MLOGIC(VAREXIST): %LOCAL RC DSID RESULT RESX
MLOGIC(VAREXIST): %LET (variable name is DSID)
SYMBOLGEN: Macro variable DS resolves to TABLE_01
MLOGIC(VAREXIST): %LET (variable name is RESX)
SYMBOLGEN: Macro variable DSID resolves to 1
SYMBOLGEN: Macro variable VAR resolves to FIELD_01
MLOGIC(VAREXIST): %LET (variable name is RC)
SYMBOLGEN: Macro variable DSID resolves to 1
SYMBOLGEN: Macro variable RESX resolves to 0
MLOGIC(VAREXIST): %IF condition &resx > 0 is FALSE
MLOGIC(VAREXIST): %LET (variable name is RESULT)
MLOGIC(VAREXIST): %PUT NOTE: Var &var not exists in &ds
SYMBOLGEN: Macro variable VAR resolves to FIELD_01
SYMBOLGEN: Macro variable DS resolves to TABLE_01
NOTE: Var FIELD_01 not exists in TABLE_01
SYMBOLGEN: Macro variable DSID resolves to 1
NOTE: Line generated by the macro variable "DSID".
5 The SAS System Friday, January 29, 2021 03:01:00 PM
124 1
_
22
200
MPRINT(VAREXIST): data 1;
SYMBOLGEN: Macro variable DSID resolves to 1
ERROR 22-322: Syntax error, expecting one of the following: a name, a quoted string, /, ;, _DATA_, _LAST_, _NULL_.
ERROR 200-322: The symbol is not recognized and will be ignored.
NOTE: Line generated by the macro variable "DSID".
124 1
_
22
200
MPRINT(VAREXIST): set 1;
SYMBOLGEN: Macro variable VAR resolves to TBL_SRC
MPRINT(VAREXIST): FIELD_01='UNK';
MPRINT(VAREXIST): run;
ERROR 22-322: Syntax error, expecting one of the following: a name, a quoted string, ;, CUROBS, END, INDSNAME, KEY, KEYRESET, KEYS,
NOBS, OPEN, POINT, _DATA_, _LAST_, _NULL_.
ERROR 200-322: The symbol is not recognized and will be ignored.
NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set WORK.DATA1 may be incomplete. When this step was stopped there were 0 observations and 19 variables.
NOTE: DATA statement used (Total process time):
real time 0.01 seconds
cpu time 0.00 seconds
MLOGIC(VAREXIST): Ending execution.
also, should i insert a line to close the table or does it just do that because i have the rc line in there?
Here you should use the dataset name, not the handle returned by the OPEN function:
data &dsid;
set &dsid;
should be
data &ds;
set &ds;
Here you should use the dataset name, not the handle returned by the OPEN function:
data &dsid;
set &dsid;
should be
data &ds;
set &ds;
Hi me55,
&dsid is a number, you cannot have data set name as a number.
Instead of
data &dsid;
set &dsid;
&var='UNK';
run;
you probably should have
data &ds;
set &ds;
&var='UNK';
run;
Hope this helps.
Remember that the OPEN function
The OPEN function opens a SAS data set, DATA step, or a SAS SQL view and returns a unique numeric data set identifier, which is used in most other data set access functions. OPEN returns 0 if the data set could not be opened.
Data set names and variable names cannot be all digits.
So
data &dsid;
is not legal when creating the value for &dsid with the Open function.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.