DATA Step, Macro, Functions and more

Generate file name conditionally in Macro

Accepted Solution Solved
Reply
Contributor
Posts: 35
Accepted Solution

Generate file name conditionally in Macro

Hi All,

I have been struggling for a long time to handle string in Macro.  I want to generate a data set name according the system file.  Simply, I want to generate a file named ABCXXX.  XXX is from ABC001 to ABC999.  If the name exists, generate the next one with number adding 1.  here is what I tried to do: (I want &DsOut is a file name that is not existing in system.)

%IF &DsOut= %THEN %DO;

       %DO I=1 %TO 999;

            %LET DsOut = %SYSFUNC(CATS("ABC_", %SYSFUNC(PUT(&I, Z3.))));

            %IF %SYSFUNC(exist(&DsOut)) NE 0 %THEN %DO;

                 %GOTO LEAVE;

                 %END;

       %END;

       %LEAVE:

  %END;

Apparently this does not work at all.  Does anyone have any suggestions or you have some example code?  Thanks.

Abdu


Accepted Solutions
Solution
‎08-27-2014 02:10 PM
Super User
Super User
Posts: 7,074

Re: Generate file name conditionally in Macro

To concatenate macro variable values you just need to reference the macro.  There is no need for a function like || or CAT() as you would have to do in regular SAS code or other programming languages.

%if &dsout= %then %do i=1 %to 999;

  %let dsout = ABC_%sysfunc(put(&i, z3.)) ;

  %if %sysfunc(exist(&dsout)) %then %goto leave;

%end;

%leave:

View solution in original post


All Replies
Solution
‎08-27-2014 02:10 PM
Super User
Super User
Posts: 7,074

Re: Generate file name conditionally in Macro

To concatenate macro variable values you just need to reference the macro.  There is no need for a function like || or CAT() as you would have to do in regular SAS code or other programming languages.

%if &dsout= %then %do i=1 %to 999;

  %let dsout = ABC_%sysfunc(put(&i, z3.)) ;

  %if %sysfunc(exist(&dsout)) %then %goto leave;

%end;

%leave:

Contributor
Posts: 35

Re: Generate file name conditionally in Macro

Hi Tom,

Thank you for your response.  I tried the code.  It renerate error message:

ERROR: The PUT function referenced in the %SYSFUNC or %QSYSFUNC macro function is not found.

I am using SAS 9.2.  Is there something I missed?

Thanks,

Abdu

Super User
Super User
Posts: 7,074

Re: Generate file name conditionally in Macro

You must use PUTN() with %SYSFUNC().  Note to apply a character format use PUTC().

Contributor
Posts: 35

Re: Generate file name conditionally in Macro

Thank you very much, Tom.

I am wondering that is there any good reference that I can read for this kind of information?

Best regards,

Abdu.

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 206 views
  • 3 likes
  • 2 in conversation