BookmarkSubscribeRSS Feed
_LB
Fluorite | Level 6 _LB
Fluorite | Level 6
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.
3 REPLIES 3
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
Try INDEXW. Or possibly explore using the SCAN or FIND functions if these may help.

Scott Barry
SBBWorks, Inc.
Cynthia_sas
SAS Super FREQ
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]
_LB
Fluorite | Level 6 _LB
Fluorite | Level 6
Cynthia;
Once again an elegant solution!
Thank you.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 741 views
  • 0 likes
  • 3 in conversation