Hi
I am changing the names of sasdata sets in a sas macro. When the new dataset name already exists the program writes an error message to my log. Which is correct. However I am trying to catch this error within the program using the &SYSERR variable. But this is always 0 after the change statment has been executed. Does running the change statment from within a macro effect the value of the &SYSERR variable. Is there another variable that I could use as this would be very handy for error checking. Code is below.
%macro ChangeFileName;
/* Change the file name of the existing files. The new file name is built using
Shared Datasets.File_Names.Archive_File_Name and the postfix returned by the %SetMMYY macro */
%PUT "------- Change File Names------";
%DO i=1 %TO &Bkup_NumObs;
%LET FileName = %SYSFUNC(CAT(&Bkup_File_Name,&i));
%LET FileNameMMYY = %SYSFUNC(CAT(&Bkup_File_Name_MMYY,&i));
%LET FileNameMMYY = &FileNameMMYY.&MMYY;
%LET Nm = %SYSFUNC(CAT(&DataLibrary,.,&FileName));
%PUT &Nm;
%IF %SYSFUNC(exist(%UPCASE(&Nm))) %THEN %DO;
%PUT Change &FileName to &FileNameMMYY;
CHANGE &FileName= &FileNameMMYY;
%PUT &syserr;
%END;
%ELSE %DO;
%PUT ******* &DataLibrary.&FileName does not exists. *******;
%END;
%END;
run;
%mend
ChangeFileName;
You can check for the existence of the data set with the following where &dsn resolves to your data set name.
%if %sysfunc(exist(&dsn)) %then %do (etc)
Probably putting your Change within another %else
I wrapped my macro call in a program and used %include program-name in placeof the call to the macro.
So for the above code I now have a program called Prog_ChangeFileName. With the code to run the macro.
PROC DATASETS Library=&DataLibrary;
%ChangeFileName;
QUIT;
I run the program and then check the syserr in a seperate macro called %chkerr
%INCLUDE automation_run_change_file_name;
%chkerr;
This has fixed my issue.
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.
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.