DATA Step, Macro, Functions and more

Not able to find out the reasoning behind this. Please Help

Accepted Solution Solved
Reply
Contributor
Posts: 35
Accepted Solution

Not able to find out the reasoning behind this. Please Help


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.


Accepted Solutions
Solution
‎05-03-2013 08:30 AM
Respected Advisor
Posts: 3,799

Re: Not able to find out the reasoning behind this. Please Help

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.

View solution in original post


All Replies
Respected Advisor
Posts: 3,799

Re: Not able to find out the reasoning behind this. Please Help

I cannot find the reasoning behind your question.

17         Data _null_;;
18            Var2='Lorem ipsum dolor';
19            Position=Find(var2,' ',-15);
20            put (_all_)(=);
21           Run;

Var2=Lorem ipsum dolor Position=
12
NOTE: DATA statement used (Total process time):
      real time          
0.00 seconds
      cpu time           
0.00 seconds
     

22         %let var2=Lorem ipsum dolor;
23         %Let position2=%sysfunc(Find(&Var2,%str( ),-15));
24         %PUT &=Position2.;
POSITION2=
12
25        
Contributor
Posts: 35

Re: Not able to find out the reasoning behind this. Please Help

Posted in reply to data_null__

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.

Respected Advisor
Posts: 3,799

Re: Not able to find out the reasoning behind this. Please Help

Maybe you need to put an ampersand in from of the variable VAR2.

Why do you need %SUBSTR?

Contributor
Posts: 35

Re: Not able to find out the reasoning behind this. Please Help

Posted in reply to data_null__

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.

Solution
‎05-03-2013 08:30 AM
Respected Advisor
Posts: 3,799

Re: Not able to find out the reasoning behind this. Please Help

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.

Contributor
Posts: 35

Re: Not able to find out the reasoning behind this. Please Help

Posted in reply to data_null__

Thanks for opening my eyes Smiley Happy 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 Smiley Happy

Thanks again for your help.

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 242 views
  • 0 likes
  • 2 in conversation