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=
Instead of a scan function, why not modify the INPUT statement to read in variables A and B directly?
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;
@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;
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,'(' ));
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,'(' ));
@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.
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.)
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.