BookmarkSubscribeRSS Feed
9 REPLIES 9
s_manoj
Quartz | Level 8

 

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

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

andreas_lds
Jade | Level 19

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.

PaigeMiller
Diamond | Level 26

@Kurt_Bremser 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
Astounding
PROC Star

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.

s_manoj
Quartz | Level 8
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
Astounding
PROC Star

@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";

s_manoj
Quartz | Level 8
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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 9 replies
  • 1084 views
  • 3 likes
  • 7 in conversation