BookmarkSubscribeRSS Feed
r3570
Obsidian | Level 7

Hi,

 

I have a variable which has value like 'XZ_VAC_901' in character format.

I have to convert this to numeric. Can anyone help ?

6 REPLIES 6
Reeza
Super User

Are they all this exact structure?

 

I would use compress to remove anything but digits and then use compress. If you have decimal points this may not work. 

 

data demo;
length value_char $15.;
value_char= 'XZ_VAC_901';
value_numeric = input(compress(value_char, , 'kd'), 8.);
run;

proc print data=demo;
run;

@r3570 wrote:

Hi,

 

I have a variable which has value like 'XZ_VAC_901' in character format.

I have to convert this to numeric. Can anyone help ?


 

r3570
Obsidian | Level 7

@Reeza 

 

My requirement was just to change the data type to numeric from character.

There is no requirement to separate out the numbers and characters.

I just need the variable type to be converted to numeric retaining the values in the variable as such.

 

XZ_VAC_901 (char) to XZ_VAC_901 (num)

 

The whole table has the values in same format as provided in above example.

 

If you can help with that it would be great.

Kurt_Bremser
Super User

"XZ_VAC" is (very obviously) not a number, so this is not possible as such.

If you have a limited, predefined scope of possible values, you could encode them in numbers and use a format to display the characters.

r3570
Obsidian | Level 7

Hi,

I have a variable which has value like 'XZ_VAC_901' in character format.
I have to convert this to numeric. Can anyone help ?
My requirement was just to change the data type to numeric from character.
There is no requirement to separate out the numbers and characters.
I just need the variable type to be converted to numeric retaining the values in the variable as such.
XZ_VAC_901 (char) to XZ_VAC_901 (num)
The whole table has the values in same format as provided in above example.
If anyone can help with this it would be great.

ballardw
Super User

@r3570 wrote:

Hi,

I have a variable which has value like 'XZ_VAC_901' in character format.
I have to convert this to numeric. Can anyone help ?
My requirement was just to change the data type to numeric from character.
There is no requirement to separate out the numbers and characters.
I just need the variable type to be converted to numeric retaining the values in the variable as such.
XZ_VAC_901 (char) to XZ_VAC_901 (num)
The whole table has the values in same format as provided in above example.
If anyone can help with this it would be great.


XZ_VAC are not numerals or digits so what you ask makes no sense whatsoever. Any character other than 123456789 and decimal, except in the case of E for exponential notation or ABCDEF for hexadecimal values, or ROMAN numerals where _ZA901 are not numerals) are not numbers. Period.

 

If someone has given you this requirement then ask them what the numeric value is.

Reeza
Super User
By what rule? What should XZ_VAC_901 map to? What's the encoding system?

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

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