BookmarkSubscribeRSS Feed
knveraraju91
Barite | Level 11
data aa;
input value $20.;
datalines;
5.00 (0.32, 1.30)
10.00 (00.32, 10.30)
;

Dear,

 

I need to split the value variable in to two variables (a   and b ). I am able to extract first one using scan function. Please suggest which modifier i need to use in my code. Thank you

 output need

a                                         b

5.00                                 (0.32, 1.30)

10.00                               (00.32, 10.30)

 

my code;

a=strip(scan(value,1,'('));

b=

7 REPLIES 7
mkeintz
PROC Star

Instead of a scan function, why not modify the INPUT statement to read in variables A and B directly?

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Shmuel
Garnet | Level 18

The delimiter to use, in your case, is '(' only:

a = strip(scan(value,1,'(' ));
b = strip(scan(value,2,'(' ));  /* or =strip(scan(value,-1,'(' )); */

@mkeintz meant:

data aa;
infile datalines dlm='(' truncover;
input a $ b $;
datalines;
5.00 (0.32, 1.30)
10.00 (00.32, 10.30)
;
run;

 

mkeintz
PROC Star

@Shmuel wrote:

...

@mkeintz meant:

data aa;
infile datalines dlm='(' truncover;
input a $ b $;
datalines;
5.00 (0.32, 1.30)
10.00 (00.32, 10.30)
;
run;

 


That's not quite what I meant, because I think the OP wanted to preserve the parentheses for variable B.  I had in mind using the "@" directive in the INPUT statement:

 

data aa;
input a :$5.0  @'(' +(-1)  b &$14. ;
put (_all_) (=);
datalines;
5.00 (0.32, 1.30)
10.00 (00.32, 10.30)
run;

Also, to read B, there has to be an "&" in the informat to prevent a single space from terminating the value.

 

Edited addition:  And on further review I don't even need the "@" directive:

data aa;
input a :$5. b :&$14.;
put (_all_) (=);
datalines;
5.00 (0.32, 1.30)
10.00 (00.32, 10.30)
run;
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
knveraraju91
Barite | Level 11

Thank you all for helping. My question was I have a variable 'value' in my data with as below. I need to split the value in to variables 'a and b'  as shown in output needed. For 'a' variable I am getting output I need. For 'b' i getting          0.32, 1.30)    and 00.32, 10.30).    

 

value

5.00 (0.32, 1.30)
10.00 (00.32, 10.30)

 

output needed

a                                                       b

5.00                                 (0.32, 1.30)

10.00                               (00.32, 10.30)

 

code 

a = strip(scan(value,1,'(' ));
b = strip(scan(value,2,'(' ));

 

mkeintz
PROC Star

Your SCAN function is using "(" as the word separator, which means it is not included as part of the word value, which is a problem for variable b.

 

You could prepend a '(' to b: 

b = cats('(',scan(value,2,'(' ));
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
andreas_lds
Jade | Level 19

@knveraraju91 wrote:
data aa;
input value $20.;
datalines;
5.00 (0.32, 1.30)
10.00 (00.32, 10.30)
;

Dear,

 

I need to split the value variable in to two variables (a   and b ). I am able to extract first one using scan function. Please suggest which modifier i need to use in my code. Thank you

 output need

a                                         b

5.00                                 (0.32, 1.30)

10.00                               (00.32, 10.30)

 

my code;

a=strip(scan(value,1,'('));

b=


Thanks for providing data in usable form!

Instead of using "scan", a regular expression could be used:

data have;
input value $20.;
datalines;
5.00 (0.32, 1.30)
10.00 (00.32, 10.30)
;

data want;
   set have;
   length a b $ 20;
   
   _rx = prxparse('/(.*\d) (.*)/');
   
   if prxmatch(_rx, value) then do;
      a = prxposn(_rx, 1, value);
      b = prxposn(_rx, 2, value);
   end;
   
   drop _rx;
run;

Maybe the first part of the expression needs tweaking, depending on the other values you have.

FreelanceReinh
Jade | Level 19

You can also use the SUBSTR function:

data want;
set aa;
a=scan(value,1,'(');
b=substr(value,length(a)+2);
run;

This assumes that there's always one blank between the two parts of the string. If the number of blanks may vary (0, 1, 2, ...), the LEFT function could compensate:

b=left(substr(value,length(a)+1));

(Note that strip(...) in the definition of a is redundant.)

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 1268 views
  • 3 likes
  • 5 in conversation