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.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 4 replies
  • 1051 views
  • 3 likes
  • 2 in conversation