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
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.
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.
Maxim 14: use the right tool. The scan() function is the right tool, so use it.
@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.
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.
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";
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.