HI All,
Please find the below code:
Data xx;
Var2='Lorem ipsum dolor';
Position=Find(%Substr(Var2,1,16),' ',-15);
%Let position2=%sysfunc(Find(%Substr(Var2,1,16),' ',-15));
Run;
%PUt &Position2.;
Now if you run the code you will find that Position and Position2 are having different values in them (Position =12 and Position2=0), but they both should produce the same result.
I can find that once you include %sysfunc before Find function it starts to give you 0 as output. Once %sysfunc is out of the equation everything is fine.
But I need to use this %sysfunc in %Let statement (need to create a Macro variable which will be used in this dataset itself. So, call symput is out of the question.) and I can not find a different way.
Most importantly I want to know why SAS is doing this? Please help.
Thnaks in advance for your efforts.
Regards,
Saugata Mukherjee.
It looks like you don't know the difference between macro and data step. You will need to consult the documentation and get a better understanding of when to use macro and the timing wrt data step execution.
Having said that I don't think you need to use macro AT ALL.
I would work with data step using find and and substr and then if you must use macro convert it to use %SYSFUNC. etc.
I cannot find the reasoning behind your question.
Hi,
Thanks a lot for your reply. But the problem comes when we add an additional substring function. Please try the below code:
%let var2=Lorem ipsum dolor;
%Let position2=%sysfunc(Find(%Substr(Var2,1,16),%str( ),-15));
%PUT &Position2.;
The log is:
407 %let var2=Lorem ipsum dolor;
408 %Let position2=%sysfunc(Find(%Substr(Var2,1,16),%str( ),-15));
WARNING: Argument 3 to macro function %SUBSTR is out of range.
409 %PUT &Position2.;
SYMBOLGEN: Macro variable POSITION2 resolves to 0
0
Where as the string is clearly more than 16 character long.
Please help me.
Regards,
Saugata Mukherjee.
Maybe you need to put an ampersand in from of the variable VAR2.
Why do you need %SUBSTR?
Yes, I am sorry I should have used ampersand in this particular case.
But I am just curious, why can't we use a variable in this statement. What I want to as is, why the below code is producing 0 as output:
Data xx;
Var2='Lorem ipsum dolor';
%Let position2=%sysfunc(Find(%Substr(Var2,1,16),%str( ),-15));
Run;
%PUt &Position2.;
Is it because SAS is considering Var2 as a string instead of a variable?
And I need substr because I want to cut the Var2 variable in two variables, each should not have more than 16 characters (Hence %substr)and no word should be cut off in between (hence finding the last space before 16th character).
The whole code is not here. Once I figure out the position of last space before 16th character, it will be pretty easy.
So, please help me to fix this, as making Var2 a Macro variable will be a cumbersome process in my situation.
Regards,
Saugata Mukherjee.
It looks like you don't know the difference between macro and data step. You will need to consult the documentation and get a better understanding of when to use macro and the timing wrt data step execution.
Having said that I don't think you need to use macro AT ALL.
I would work with data step using find and and substr and then if you must use macro convert it to use %SYSFUNC. etc.
Thanks for opening my eyes I was using structure from a old code and in doing so I completely overlooked that I do not need to create a macro variable, instead dropping the postion variable would be enough. Now it is working perfectly.
I guess sometimes you miss out small things
Thanks again for your help.
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.