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

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?

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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(,),','));

 

View solution in original post

4 REPLIES 4
PaigeMiller
Diamond | Level 26
%let id=%sysfunc(catq(1ac,%bquote(&id)));
--
Paige Miller
PeterClemmensen
Tourmaline | Level 20

Use the %Bquote Function on the macro variable.

Tom
Super User Tom
Super User

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(,),','));

 

maguiremq
SAS Super FREQ

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.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 1024 views
  • 2 likes
  • 5 in conversation