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.
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.
Ready to level-up your skills? Choose your own adventure.