DATA Step, Macro, Functions and more

INDEX VS RXPARSE & numerical characters

Reply
Regular Contributor
Regular Contributor
Posts: 170

INDEX VS RXPARSE & numerical characters

Hello SAS forum;

OK, so I am trying to parse specific strings of numbers into individual variables so that I have for example

1,2,3
1,4,5
1,5,7
3,10
etc etc

Here is my problem, if I try to use the index or Rxparse functions for the characters '10' I still get a 1 if 1 is anywhere in the string. Is there a way to do a literal string match that matches exactly the '10' but not the '1'
Thank you.
Super Contributor
Super Contributor
Posts: 3,174

Re: INDEX VS RXPARSE & numerical characters

Try INDEXW. Or possibly explore using the SCAN or FIND functions if these may help.

Scott Barry
SBBWorks, Inc.
SAS Super FREQ
Posts: 8,744

Re: INDEX VS RXPARSE & numerical characters

Hi:
Have you looked at the SCAN function???? If the numbers are -reliably- delimited by ',' (comma), then I'd recommend SCAN...probably inside a do loop. I treated the initial "line" of comma separated data as a character string and then used the SCAN function to split out the numbers. Automatic conversion took place from character to numeric -- which you may want to further control with an INPUT function in the assignment statement. But for a simple example, I didn't bother with an INPUT. I also only showed the comma as a delimiter for the SCAN -- it has a set of defaults that it would otherwise use. You can find those in the doc. The only downside to SCAN that I can think of is that 2 contiguous delimiters would be treated as 1 delimiter.

If you already have the data in a character variable, then SCAN is what I'd recommend.

If you have the data in a CSV file and want to READ the data into SAS, then I'd recommend using the delimiter= option on the infile statement. Both techniques are shown below.

cynthia
[pre]
**1) Use SCAN function on character string of numbers delimited by commas;
data parse_nums;
length nline $100;
infile datalines;
input nline $;
return;
datalines;
1,2,3
1,4,5
1,5,7
3,10
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15
;
run;

data separate;
set parse_nums;
array sv num1-num20;
do i = 1 to dim(sv);
sv(i) = scan(nline,i,',');
end;
run;

proc print data=separate;
run;

**2) READ data into numeric vars;
data read_nums;
infile datalines delimiter=',' missover;
input var1-var20;
return;
datalines;
1,2,3
1,4,5
1,5,7
3,10
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15
;
run;

proc print data=read_nums;
run;
[/pre]
Regular Contributor
Regular Contributor
Posts: 170

Re: INDEX VS RXPARSE & numerical characters

Cynthia;
Once again an elegant solution!
Thank you.
Ask a Question
Discussion stats
  • 3 replies
  • 129 views
  • 0 likes
  • 3 in conversation