|
View previous topic :: View next topic
|
| Author |
Message |
vasanthz
Global Moderator

Joined: 28 Aug 2007 Posts: 1753 Location: Tirupur, India
|
|
|
|
Hello,
I am trying to extract the 2nd word from a string in SAS.
The words are delimited by ,+ (both of them, not ',' or '+')
For input data
the second word that needs to be extracted is "444"
I tried the below code, but scan function treats "," or "+" as delimiters not a combination of them.
Code:
| Code: |
data practice;
var1 = '111,22+333,+444';
val = scan(var1,2,',+');
put val=;
run; |
Output:
Please let me know how this could be done. Thanks. |
|
| Back to top |
|
 |
expat
Global Moderator

Joined: 14 Mar 2007 Posts: 8798 Location: Welsh Wales
|
|
|
|
| Have you looked at DSD and DLM='+', just a first thought |
|
| Back to top |
|
 |
Robert Sample
Global Moderator

Joined: 06 Jun 2008 Posts: 8700 Location: Dubuque, Iowa, USA
|
|
|
|
| Code: |
data practice;
var1 = '111,22+333,+444';
delimloc = index(var1, ',+');
if delimloc > 0 then val = substr(var1, delimloc+2);
|
|
|
| Back to top |
|
 |
vasanthz
Global Moderator

Joined: 28 Aug 2007 Posts: 1753 Location: Tirupur, India
|
|
|
|
@Robert, Thanks for your code, it works well.
@Expat, Thanks.. I was trying to extract data from a variable, so DLM & DSD infile options were not useful in this scenario.
Regards, |
|
| Back to top |
|
 |
PeterHolland
Global Moderator

Joined: 27 Oct 2009 Posts: 2481 Location: Netherlands, Amstelveen
|
|
| Back to top |
|
 |
expat
Global Moderator

Joined: 14 Mar 2007 Posts: 8798 Location: Welsh Wales
|
|
|
|
Sorry, I read your post in a hurry.
tut tut, should know better next time |
|
| Back to top |
|
 |
vasanthz
Global Moderator

Joined: 28 Aug 2007 Posts: 1753 Location: Tirupur, India
|
|
|
|
@Peter Thanks for the link,
I had a look at the link before starting the topic, but I think the modifiers for SCAN function applies for latest release of SAS :S ..
We have SAS 9.1 and when I use the modifiers for the scan function it says,
| Code: |
ERROR 72-185: The COUNTW function call has too many arguments.
8 do count = 1 to nwords;
9 word = scan(string, count, delim, modif);
----
72
ERROR 72-185: The SCAN function call has too many arguments. |
For the program,
| Code: |
data comma;
keep count word;
length word $30;
string = ',leading, trailing,and multiple,,delimiters,,';
delim = ',';
modif = 'mo';
nwords = countw(string, delim, modif);
do count = 1 to nwords;
word = scan(string, count, delim, modif);
output;
end;
run;
proc print data=comma noobs;
run; |
Regards, |
|
| Back to top |
|
 |
vasanthz
Global Moderator

Joined: 28 Aug 2007 Posts: 1753 Location: Tirupur, India
|
|
|
|
Hello,
For the benefit of sharing, There was one more function which was useful in this scenario along with Robert's solution,
| Code: |
data practice;
var1 = '111,22+333,+444';
temp = tranwrd(var1,',+','~');
val = scan(temp,2,'~');
put val=;
run; |
|
|
| Back to top |
|
 |
PeterHolland
Global Moderator

Joined: 27 Oct 2009 Posts: 2481 Location: Netherlands, Amstelveen
|
|
|
|
| Quote: |
I had a look at the link before starting the topic, but I think the modifiers for SCAN function applies for latest release of SAS :S ..
We have SAS 9.1
|
SAS 9.1 :
support.sas.com/publishing/pubcat/chaps/59343.pdf
Search for CALL SCAN in that PDF. |
|
| Back to top |
|
 |
|
|
 |
All times are GMT + 6 Hours |
|