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
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:
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:
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
You must use PUTN() with %SYSFUNC(). Note to apply a character format use PUTC().
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.
Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.
Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.
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.