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

hi there!

 

I have to concat a string in a %let variable, however when i use '0' is the same that zero don't exist.

 

follow my code:

 

%macro b();
        
           %let a=202102 ;
         	%let a = %sysfunc(catt(%sysfunc(substr(&a.,1,4)),%STR(01)));
           %put &a;
        
%mend b;
%pippo;

 the result should be: 202101 however i got 20211.

If i change '0' to any other number it works.

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Way too much code:

 %let a=202102 ;
 %let a = %substr(&a.,1,4)01;
 %put &a;

First, there  is a macro %substr function so no reason to go use %sysfunc for that. The way the macro processor works strings are pretty much appended as is unless one of them might contain macro trigger, i.e. & or %.

You often need to use the Macro processor concatenate operator . (dot) when appending text directly to a macro variable  as in : %let newvar = &var.sometext; otherwise the macro processor looks for a macro variable named Varsometext (probably not finding it).

View solution in original post

3 REPLIES 3
ballardw
Super User

Way too much code:

 %let a=202102 ;
 %let a = %substr(&a.,1,4)01;
 %put &a;

First, there  is a macro %substr function so no reason to go use %sysfunc for that. The way the macro processor works strings are pretty much appended as is unless one of them might contain macro trigger, i.e. & or %.

You often need to use the Macro processor concatenate operator . (dot) when appending text directly to a macro variable  as in : %let newvar = &var.sometext; otherwise the macro processor looks for a macro variable named Varsometext (probably not finding it).

pink_poodle
Barite | Level 11
This works:
%let a = %sysfunc(catt(%sysfunc(substr(&a.,1,4)),%STR(0),%STR(1)));
Tom
Super User Tom
Super User

There is no need to use CATxx() function in macro code.  Macro code is just text substitution!  So just put the text where you want it to appear.

%let a = %substr(&a.,1,4)01;

And to make things worse the %SYSFUNC() macro function does not play that well with SAS functions that allow either numeric or character values for some of their arguments.  Like the CATxxx() function do.  This is because everything is a text to the macro processor, but the SAS functions want to know whether they are being given a number or a string.  When you call them in a data step they know what you are giving them.  So %SYSFUNC() tries to GUESS whether the text in your macro call should be considered a number or a string. The text 01 looks like a number.  And there is no different between the numbers 1 and 01 and 00001 they all mean the same number.  So CATT() says, thanks I'll convert the number 1 into a string and concatenate it to the other arguments.  And poof their goes your 0 digit.

 

 

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
  • 3 replies
  • 1186 views
  • 1 like
  • 4 in conversation