if statement - SAS: update dataset if it exists, else create it -
i need update dataset mylib.dt_old using dataset dt_new, want first check if exists, otherwise error. in case not exist, want create dataset mylib.dt_old stores information contained in dt_new. tried following:
if (exist(mylib.dt_old)) do; data mylib.dt_old; update mylib.dt_old dt_new; date var1 var2; run; end; else do; data mylib.dt_old; set dt_new; run; end;
however error, when mylib.dt_old not exist
error: file mylib.dt_old.data not exist.
and sas goes on executing else statement (creates dt_old copying dt_new).
instead if dt_old exist
if (exist(mylib.dt_old)) -- 180 error 180-322: statement not valid or used out of proper order.
what doing wrong? ps: beginner in sas.
what you're trying qualify macro programming in sas, considered "advanced" topic.
the normal if-then control logic you're using sas considers data-step code, i.e. it's valid in data step. there "macro" language looks similar, distinguished %
prefixes. here's how modified code might look:
%macro updater(); %if %sysfunc(exist(mylib.dt_old)) %then %do; data mylib.dt_old; update mylib.dt_old dt_new; date var1 var2; run; %end; %else %do; data mylib.dt_old; set dt_new; run; %end; %mend updater; %updater()
while example sort-of straightforward, sas macro language counterintuitive (to me @ least). if you're beginner might idea find , read "into sas macros" online.
Comments
Post a Comment