IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

Extracting delimited values - SAS


IBM Mainframe Forums -> All Other Mainframe Topics
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
vasanthz

Global Moderator


Joined: 28 Aug 2007
Posts: 1742
Location: Tirupur, India

PostPosted: Tue Jun 21, 2011 7:17 pm
Reply with quote

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
Code:
111,22+333,+444

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:
Code:
val=22


Please let me know how this could be done. Thanks.
Back to top
View user's profile Send private message
expat

Global Moderator


Joined: 14 Mar 2007
Posts: 8797
Location: Welsh Wales

PostPosted: Tue Jun 21, 2011 7:27 pm
Reply with quote

Have you looked at DSD and DLM='+', just a first thought
Back to top
View user's profile Send private message
Robert Sample

Global Moderator


Joined: 06 Jun 2008
Posts: 8696
Location: Dubuque, Iowa, USA

PostPosted: Tue Jun 21, 2011 7:28 pm
Reply with quote

Code:
data practice;           
var1 = '111,22+333,+444';
delimloc = index(var1, ',+');
if delimloc > 0 then val = substr(var1, delimloc+2);
Back to top
View user's profile Send private message
vasanthz

Global Moderator


Joined: 28 Aug 2007
Posts: 1742
Location: Tirupur, India

PostPosted: Tue Jun 21, 2011 7:57 pm
Reply with quote

@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
View user's profile Send private message
PeterHolland

Global Moderator


Joined: 27 Oct 2009
Posts: 2481
Location: Netherlands, Amstelveen

PostPosted: Tue Jun 21, 2011 8:44 pm
Reply with quote

support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000214639.htm
Back to top
View user's profile Send private message
expat

Global Moderator


Joined: 14 Mar 2007
Posts: 8797
Location: Welsh Wales

PostPosted: Tue Jun 21, 2011 9:42 pm
Reply with quote

Sorry, I read your post in a hurry.

tut tut, should know better next time
Back to top
View user's profile Send private message
vasanthz

Global Moderator


Joined: 28 Aug 2007
Posts: 1742
Location: Tirupur, India

PostPosted: Wed Jun 22, 2011 12:19 pm
Reply with quote

@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
View user's profile Send private message
vasanthz

Global Moderator


Joined: 28 Aug 2007
Posts: 1742
Location: Tirupur, India

PostPosted: Wed Jun 22, 2011 12:26 pm
Reply with quote

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
View user's profile Send private message
PeterHolland

Global Moderator


Joined: 27 Oct 2009
Posts: 2481
Location: Netherlands, Amstelveen

PostPosted: Wed Jun 22, 2011 2:09 pm
Reply with quote

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
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> All Other Mainframe Topics

 


Similar Topics
Topic Forum Replies
No new posts INCLUDE OMIT COND for Multiple values... DFSORT/ICETOOL 5
No new posts Extracting Variable decimal numbers f... DFSORT/ICETOOL 17
No new posts Replace Multiple Field values to Othe... DFSORT/ICETOOL 12
No new posts Null values are considered in Total c... DFSORT/ICETOOL 6
No new posts Help in extracting data between doubl... DFSORT/ICETOOL 5
Search our Forums:

Back to Top