DATA Step, Macro, Functions and more

how to derive third word from a string without using scan function

Reply
Frequent Contributor
Posts: 88

how to derive third word from a string without using scan function

 
Contributor
Posts: 50

Re: how to derive third word from a string without using scan function

Posted in reply to thanikondharish

 

Hi,

please check below code

 

data want;
x = "hello how are you";
p = anyspace(x,anyspace(x)+1);
p1 = anyspace(x,p+1);
word3 = substr(x,p,p1-p);
run;

Regards,

manoj

Super User
Super User
Posts: 9,866

Re: how to derive third word from a string without using scan function

Posted in reply to thanikondharish

Please use the body of the thread post to describe your problem exactly.  Present test data in the form of a datastep, and show what the output should look like.  

From what you have posted, you would simply supply 3 to the function, e.g.:

want=scan(have,3," ");

I have assumed space delimiter.

Valued Guide
Posts: 631

Re: how to derive third word from a string without using scan function

Posted in reply to thanikondharish

You could use a regular expression to extract the third word from a string, see the documentation of prxparse and prxposn for details. But i don't see any reason not to use scan-function. It is faster and requires less code.

Super User
Posts: 10,623

Re: how to derive third word from a string without using scan function

Posted in reply to thanikondharish

Maxim 14: use the right tool. The scan() function is the right tool, so use it.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Respected Advisor
Posts: 3,294

Re: how to derive third word from a string without using scan function

Posted in reply to KurtBremser

@KurtBremser wrote:

Maxim 14: use the right tool. The scan() function is the right tool, so use it.


OFF TOPIC VENTING

I get frustrated by the number of questions asked here in the SAS Communities where the user states that a specific tool has to be used, or the user states that the best tool not be used. I find myself replying to these questions only on rare occasions now. So Maxim 14 is a good one.

 

I make an exception if the best tool is not in your SAS license, for example you don't have PROC IML in your SAS license, that's a different situation. But this exception doesn't apply to SCAN.

--
Paige Miller
Super User
Posts: 6,939

Re: how to derive third word from a string without using scan function

Posted in reply to thanikondharish

You've already been warned about your refusal to use SCAN.  It's like saying, "I want to dig a ditch and I have to use a spoon instead of a shovel."  Just to put that in context here ...

 

The @RW9 shows you how to use SCAN.  It works.

 

The @s_manoj solution avoids SCAN and uses a good set of tools, but not a foolproof set of tools.  Try using it on this sentence:

 

x = "hello  how are you"

 

The extra blank throws off the logic and selects "how" as the third word.  So you could use an even more complex approach, or you could just use SCAN, the tool that is built for the job.

Contributor
Posts: 50

Re: how to derive third word from a string without using scan function

Posted in reply to Astounding
Hi @Astounding
yes with above sentence you have , that code might give different result,
If we add compbl() function to the above code, then problem would be solved
Super User
Posts: 6,939

Re: how to derive third word from a string without using scan function

@s_manoj,

 

Yes, I agree COMPBL would help.  I wasn't trying to pick on your solution in particular ... just trying to illustrate that SCAN eliminates complications because it is built for the job.  Even with COMPBL, you might need LEFT also.  Try adding a leading blank and see what happens:

 

x = " hello how are you";

Contributor
Posts: 50

Re: how to derive third word from a string without using scan function

Posted in reply to Astounding
Hi @Astounding,
Yes I completely agree with you and that's true, it's better not to complicate code when a simple function can do. In recent days some of them were coming across these type logical questions by there superiors, so that might be the reason they were posting these type of questions, but up to they won't implement these type of complicated code in real scenarios.

Thankyou
Ask a Question
Discussion stats
  • 9 replies
  • 132 views
  • 3 likes
  • 7 in conversation