BookmarkSubscribeRSS Feed
K_S
Obsidian | Level 7 K_S
Obsidian | Level 7

Hello, 

I need to convert a numeric variables (all the numbers are whole numbers, no decimals) to character because I need to insert some character symbols and I seem to be able to do the conversion using compress. i.e. new=compress(old). This seems to work. Is this bad practice? Can using compress for this num to char conversion introduce some sort of errors that I am not aware of?

6 REPLIES 6
Kurt_Bremser
Super User

COMPRESS is a character function, so you should not use it on numbers (unnecessary NOTE in the log).

Use a PUT function with a proper format and the left-alignment modifier.

If you need to insert characters at fixed positions in numbers with consistent numbers of digits, create a picure format.

Tom
Super User Tom
Super User

The COMPRESS() function is for removing characters from a string.  So the COMPRESS() function works on character arguments, not on numbers. (what would it even mean on a number?)

But SAS will automatically convert your number into a character, which will generate notes to the low.

To avoid the notes use the PUT() function to convert the number into a character string.  That way you can use the proper format.

For example:

put(old,17.)

If you do not give COMPRESS the optional second argument it will remove the spaces from the string.  The only spaces PUT will generate with the format will be leading spaces.  You can remove the leading spaces using the LEFT() function (you can ignore any trailing spaces as SAS stores character variables as fixed length values padded with spaces).

new=left(put(old,17.));

You can also use the -L modifier on the format to have PUT left align the string for you.

new=put(old,17.-L);
K_S
Obsidian | Level 7 K_S
Obsidian | Level 7
I’m not sure what the proper format is. The length is 8, can I use 8. as the format?
AMSAS
SAS Super FREQ

Here's the documentation on PUTN function 
There's links to the PUT function too, and some examples

Tom
Super User Tom
Super User

The LENGTH of a variable in SAS is how many bytes it takes to store the variable in the dataset. Since numbers are always stored as floating point the length of 8 is the default (you can elect to use fewer bytes to store the numbers but that just loses precision on the number being stored. )

 

The width of the format to use when converting the number to a character string depends on the values that the variable could hold.  If the values are integers then the largest integer that can be represent exactly contiguously is 9,007,199,254,740,992 (at least on systems using IEEE floating point numbers) which would need 16 characters.  But you might also have a negative value.  So by using the format of 17. you can represent any integer you might have stored (or at least any that you could consistently store and retrieve).

3879  data _null_;
3880    x=constant('exactint');
3881    put x comma24.;
3882  run;

   9,007,199,254,740,992

 

Kurt_Bremser
Super User

When one wants to insert characters into numbers, this usually happens at fixed positions within a fixed number of digits.

Please give us some examples of the numbers you have and how you want them to appear in the end.

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