Dear,
I need help in my pgm to get the output i need. I need to create a numeric variable 'b' from char variable 'a'
output need
15.22
33.22
please suggest. Thank you
data have;
input a $20.;
datalines;
CYCLE 15 DAY 22
CYCLE 33 DAY 22
;
data want;
set have;
b=compress(a,'','kd');
run;
data have;
input a $20.;
datalines;
CYCLE 15 DAY 22
CYCLE 33 DAY 22
;
data want;
set have;
b=catx('.',scan(a,1,,'kd'),scan(a,2,,'kd'));
run;
You've got the right idea using the COMPRESS function with 'kd' as the third parameter. Then you would want to use the INPUT function (function not statement) to cast the data into numeric form.
Like this:
data have;
input a $20.;
datalines;
CYCLE 15 DAY 22
CYCLE 33 DAY 22
;
data want;
set have;
b = INPUT(TRANWRD(strip(COMPBL(compress(a,,'kd'))),' ', '.'), 8.2);
run;
Which yields:
Are you sure that's what you want? It almost seems like cycle and day should be two separate fields instead of a single numeric fields with two values, in essence, separated by a decimal point.
Jim
I suspect you really want two variables, CYCLE and DAY, and likely separately.
CYCLE = input(scan(a, 2), 8.);
DAY = input(scan(a, 4), 8.);
@knveraraju91 wrote:
Dear,
I need help in my pgm to get the output i need. I need to create a numeric variable 'b' from char variable 'a'
output need
15.22
33.22
please suggest. Thank you
data have; input a $20.; datalines; CYCLE 15 DAY 22 CYCLE 33 DAY 22 ; data want; set have; b=compress(a,'','kd'); run;
data have;
input a $20.;
datalines;
CYCLE 15 DAY 22
CYCLE 33 DAY 22
;
data want;
set have;
b=catx('.',scan(a,1,,'kd'),scan(a,2,,'kd'));
run;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.