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


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.

1 ACCEPTED SOLUTION

Accepted Solutions
data_null__
Jade | Level 19

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

6 REPLIES 6
data_null__
Jade | Level 19

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        
Saugata
Obsidian | Level 7

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.

data_null__
Jade | Level 19

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

Why do you need %SUBSTR?

Saugata
Obsidian | Level 7

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.

data_null__
Jade | Level 19

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.

Saugata
Obsidian | Level 7

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.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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
  • 6 replies
  • 932 views
  • 0 likes
  • 2 in conversation