BookmarkSubscribeRSS Feed
AndrewOS
Fluorite | Level 6

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;

2 REPLIES 2
ballardw
Super User

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

AndrewOS
Fluorite | Level 6

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.

sas-innovate-2024.png

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.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 1236 views
  • 0 likes
  • 2 in conversation