BookmarkSubscribeRSS Feed
khushi
Calcite | Level 5

My file name is qa_test_r00. It is flat file. I want to change it to qa_test_r01 if qa_test_r00 already exists.Then to qa_test_r02 next time once file r01 is created. Can you help me with it?  I tried using below snippet but its not working beyond r01 version.

options mprint mtrace mlogic macrogen symbolgen notes source source2;

%MACRO FILNAMES;
  %LET _ASOFDT1 = 20181019;
  %LET VER = 0;
  FILENAME _dtltrad "CAPP_1Q18_ACAS_Detail_R0&VER._&_ASOFDT1._Primary" ;
  %IF &sysfilrc > 0 %then %DO;
  %LET VER = %EVAL(&VER +1);
  %LET _DTLTRAD  = "CAPP_1Q18_ACAS_Detail_R0&VER._&_ASOFDT1._Primary" ;
  %END;

  %ELSE %DO;
  %LET _DTLTRAD  = "CAPP_1Q18_ACAS_Detail_R0&VER._&_ASOFDT1._Primary" ;
  %END;
  %PUT &_DTLTRAD;
  %PUT &VER.;
  FILENAME _dtltrad &_DTLTRAD;
%MEND FILNAMES;

%FILNAMES;

 

Thanks in advance

2 REPLIES 2
RW9
Diamond | Level 26 RW9
Diamond | Level 26

As with your other post in terms of code formatting.  With regards to this, why do you want to increment filename?  Would not a version control system or something specifically designed for the task not be better?  Just thinking your going to have to code various checks to ensure this works ok, is it worth it.  For instance, what if a file gets removed, or needs replacing etc.  This isn't to say it can't be done with something like:

data _null_;
  do i=1 to 100;  /* Set 100 to something big */
    if fileexist(cats("capp_1q18_acas_detail_r0",put(i,best.),"_primary.txt"))=0 then do;
      call symputx('next',i);
      leave;
    end;
  end;
run;

%put Next available number is: &next.;
ballardw
Super User

@khushi wrote:

My file name is qa_test_r00. It is flat file. I want to change it to qa_test_r01 if qa_test_r00 already exists.Then to qa_test_r02 next time once file r01 is created. Can you help me with it?  I tried using below snippet but its not working beyond r01 version.

options mprint mtrace mlogic macrogen symbolgen notes source source2;

%MACRO FILNAMES;
  %LET _ASOFDT1 = 20181019;
  %LET VER = 0;
  FILENAME _dtltrad "CAPP_1Q18_ACAS_Detail_R0&VER._&_ASOFDT1._Primary" ;
  %IF &sysfilrc > 0 %then %DO;
  %LET VER = %EVAL(&VER +1);
  %LET _DTLTRAD  = "CAPP_1Q18_ACAS_Detail_R0&VER._&_ASOFDT1._Primary" ;
  %END;

  %ELSE %DO;
  %LET _DTLTRAD  = "CAPP_1Q18_ACAS_Detail_R0&VER._&_ASOFDT1._Primary" ;
  %END;
  %PUT &_DTLTRAD;
  %PUT &VER.;
  FILENAME _dtltrad &_DTLTRAD;
%MEND FILNAMES;

%FILNAMES;

 

Thanks in advance


Please make sure that your question text aligns somewhat with any example code.

You say that you want to basically use a file name similar to "qa_test_r01"
 but you code is going to generate names like "CAPP_1Q18_ACAS_Detail_R0120181019_Primary".

When question and code do not come close then we spend a lot of time guessing which is actually meant.

 

Do you actually make multiple files on the same date (assumes use of _ASOFDT1 as a date, not mentioned or defined in question text).

 

Note that if you have this run more than 9 times you are going to have _r010 names, which will seriously mess with any sort order of names.

 

Why only -ro1: you only tell it to do one iteration. There is no repeat loop in your code. I suspect you may have been thinking you were doing either a DO UNTIL or DO WHILE such as

 

%IF &sysfilrc > 0 %then %DO %until &sysfilrc=0;
  %LET VER = %EVAL(&VER +1);
  %LET _DTLTRAD  = "CAPP_1Q18_ACAS_Detail_R0&VER._&_ASOFDT1._Primary" ;
  FILENAME _dtltrad &_DTLTRAD;
%END;

 

without the %else %do block.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!

Submit your idea!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 597 views
  • 0 likes
  • 3 in conversation