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: Call for Content

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!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 604 views
  • 6 likes
  • 4 in conversation