Hi,
I wanted to know how can we check whether a column is present in a table or not using macro?
And i want to update that table if it is present in the table.
So I will have to use Proc SQL as well.
Thanks in advance, waiting for a quick reply!
You can use next code in a macro program to check does variable exist in a table:
%let dsid=%sysfunc(open(table_in,i));
%let var_num=%sysfunc(varnum(&dsid,VARIABLE_NAME));
%if %eval(&VAR_num) = 0 %then %do;
<variable does not exit>
%end;
%else do;
<variable exists>
%end;
you can even check it in a datastep:
data table_out;
set table_in;
dsid = open(table_in,i);
var_num = varnum(dsid,'<variable name>');
if var_num > 0 then do;
<variable exists>
end;
........
run;
Hi,
Thanks for the code, but when I'm using Proc SQL fro updating the table, I am getting some errors.
I am using the following code, where employee is the name of my table and Emp_ID is one of the variable.
%let dsid=%sysfunc(open(employee)); %let var_num=%sysfunc(varnum(&dsid,Emp_Add)); proc sql; %if %eval(&var_num) >0 %then %do; update employee as a set Emp_ID = Emp_ID * 100; end; %else %do; select account_number from Employee_Salary %end; %quit; |
It says %IF, %else are not valid in open code!
You cannot use macro statements, like %IF, in an open code - neither in a data step nor in a sql code.
You have to enclose it in a macro program:
%macro macro_name(<any arguments>);
....
proc sql;
.........
%if <any condition> %then %do;
.......
%end;
.......
quit;
%mend;
%macro_name(<supply arguments>);
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.