- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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).
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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).
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
%let a = %sysfunc(catt(%sysfunc(substr(&a.,1,4)),%STR(0),%STR(1)));
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.