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;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.