BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
knveraraju91
Barite | Level 11

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;
1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
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;

View solution in original post

3 REPLIES 3
jimbarbour
Meteorite | Level 14

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:

jimbarbour_0-1628546588416.png

 

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

Reeza
Super User

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;

 

Ksharp
Super User
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: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 3 replies
  • 679 views
  • 6 likes
  • 4 in conversation