In the program below, leading zeros are getting truncated but I want to keep leading zeros in the ID variable.
%let id=0012,0333; %let id=%sysfunc(catq(1ac,&id)); %put #####&id.;
Log:
26 %let id=0012,0333; 27 %let id=%sysfunc(catq(1ac,&id)); 28 %put #####&id.; #####'12','333'
Desired Result:
Id should resolve to '0012','0333'
Any help?
It is because you are trying to use a data step function on your macro variable values. In particular one that can take either numeric or character values for its arguments.
But why would you ever call any CATxxx function in macro code? To concatenate macro variable values just expand them next to each other.
To do this transformation just change the commas to quoted commas and add quotes on the outside. Easier with double quotes.
%let id=0012,0333;
%put %sysfunc(tranwrd("&id",%str(,),","));
If you need single quotes:
%let id=0012,0333;
%put %sysfunc(tranwrd(%bquote('&id'),%str(,),','));
%let id=%sysfunc(catq(1ac,%bquote(&id)));
Use the %Bquote Function on the macro variable.
It is because you are trying to use a data step function on your macro variable values. In particular one that can take either numeric or character values for its arguments.
But why would you ever call any CATxxx function in macro code? To concatenate macro variable values just expand them next to each other.
To do this transformation just change the commas to quoted commas and add quotes on the outside. Easier with double quotes.
%let id=0012,0333;
%put %sysfunc(tranwrd("&id",%str(,),","));
If you need single quotes:
%let id=0012,0333;
%put %sysfunc(tranwrd(%bquote('&id'),%str(,),','));
Is there a reason that you have to define them with a LET statement? If you have them in a data set, it's probably easier to do this, and it gives you the output you need. More of a data-driven technique.
data have;
input id $;
datalines;
0012
0333
;
run;
proc sql noprint;
select
quote(trim(id))
into: ids separated by ","
from
have;
quit;
%put ####&ids.;
%put ####&ids.; ####"0012","0333"
I'm not terribly experienced with manipulating data with macro quoting functions, so someone else may have better insight than my solution.
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 25. Read more here about why you should contribute and what is in it for you!
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.