Hello
I have a string that have 3 words with two commas between them.
I want to calculated 3 fields- continent, city,country.
Can anyone explain why City=scan(X,-2,","); result with city?
In this case there are 2 commas, to which comma this code refer to? to first or second?
As I understand since -2 is negative then it search for the word in right side of the comma?
data have;
input X $20.;
cards;
EU,Athens,Greece
Asia,Beijing,China
;
run;
data want;
set have;
continent=scan(X,1,",");/**EU**/
City=scan(X,-2,",");/**Athens**/
Country=scan(X,-1,",");/**Greece**/
Run;
You don't have the doc?
It's on www as well.
Functions and CALL Routines
SCAN Function
Koen
My question was what happens if there are multiple delimiters? Then which delimiter is the reference? The first one from left side?
If there are multiple delimiters, they are all reduced to a common delimiter (also in the strings to scan) and then the scan is performed on that common delimiter. I don't know if that's really how it happens in the background (probably not), but that's how to think about it.
data have;
input X $20.;
cards;
EU,Athens#Greece
Asia#Beijing,China
;
run;
data want;
set have;
continent = scan(X, 1,",#");/**EU**/
City = scan(X,-2,",#");/**Athens**/
Country = scan(X,-1,",#");/**Greece**/
run;
Koen
Ah oké, that's what @Ronein means with multiple delimiters.
Indeed ... answer provided by @Ksharp in that case.
Here's the doc about that m (or M) modifier:
m or M | specifies that multiple consecutive delimiters, and delimiters at the beginning or end of the string argument, refer to words that have a length of zero. If the M modifier is not specified, then multiple consecutive delimiters are treated as one delimiter, and delimiters at the beginning or end of the string argument are ignored. |
Koen
My question was what happens if there are multiple delimiters? Then which delimiter is the reference? The first one from left side?
Try it and see.
@Ronein wrote:
My question was what happens if there are multiple delimiters? Then which delimiter is the reference? The first one from left side?
It is not referencing the delimiters. It is referencing the words that are formed by splitting the string at the delimiters. Consider a string with 5 words. Here are some example 5 word strings when the comma is the delimiter.
one,two,three,four,five
This sentence,is divided,into,five,parts
You can either count from left to right but using positive indexes. Or count from right to left by using negative indexes.
Word Positive Negative one 1 -5 two 2 -4 three 3 -3 four 4 -2 five 5 -1
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.