BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Abdu
Calcite | Level 5

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

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

4 REPLIES 4
Tom
Super User Tom
Super User

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:

Abdu
Calcite | Level 5

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

Tom
Super User Tom
Super User

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

Abdu
Calcite | Level 5

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.

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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