BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
sebastiaam
Obsidian | Level 7

Hi, I am trying to copy a number from one column to another

data MS_MAIN_INPUT;
set MS_MAIN_INPUT_0;
retain new_CPFN;
if MARCA="A" then new_CPFN = CPFN;
if MARCA="N" then new_CPFN = CPFN_old;
run;

But it seems that the number loses some digits when doing the copy.

sebastiaam_0-1674588497528.png

If I export to CSV this table I see that the number is different

sebastiaam_1-1674588587188.png

sebastiaam_2-1674588607298.png

How can I keep all the digits?

 

Thanks for your help!

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Assign a proper format (e.g. 32.).

But your numbers with 16 digits are right on the verge of numeric precision (SAS stores numbers in 8 byte real).

If these are in fact codes and not numbers, they should be stored as character when the data is read into SAS.

View solution in original post

6 REPLIES 6
PaigeMiller
Diamond | Level 26

SAS cannot represent exactly numbers that are 15 digits long (or longer), and so "rounding" occurs.

 

Numbers such as this should be character strings, and then the problem goes away. 

--
Paige Miller
Kurt_Bremser
Super User

Assign a proper format (e.g. 32.).

But your numbers with 16 digits are right on the verge of numeric precision (SAS stores numbers in 8 byte real).

If these are in fact codes and not numbers, they should be stored as character when the data is read into SAS.

sebastiaam
Obsidian | Level 7

Thank You!

I am trying to change into character but I don't get the whole number. How can I do it? 

sebastiaam_0-1674592900073.png

data want;
set tables;
retain new_CPFN;
if MARCA="A" then new_CPFN = put(CPFN, 18.);
if MARCA="N" then new_CPFN = put(CPFN_old, 18.);
run;

 


 

Tom
Super User Tom
Super User

Looks like you already defined NEW_CPFN in the source dataset and the length defined for it is not long enough to store an 18 digit string.

 

Check this example:

2892   data test;
2893     length number1 number2 8 string1 string2 $8 string3 $18;
2894     number1=1234567890123 ;
2895     number2=put(number1,18.);
2896     string1=put(number1,18.);
2897     string2=put(number1,18.-L);
2898     string3=put(number1,18.);
2899     format number: best18. string: $quote. ;
2900     put (_all_) (=/);
2901   run;

NOTE: Character values have been converted to numeric values at the places given by: (Line):(Column).
      2895:11

number1=1234567890123
number2=1234567890123
string1="     123"
string2="12345678"
string3="     1234567890123"
sebastiaam
Obsidian | Level 7

Thanks! That was it!

ballardw
Super User

Are you doing arithmetic with the variables CPFN or new_CPFN? If not, I would go back to the initial read data into SAS step and make sure that CPFN is character.

BTW, if the CPFN or other variable had leading zeroes then the read as numeric lost those. Which means that values in the original source such as "000123" and  "0000000000123" would both now be numeric 123 and likely wrong for some purposes.

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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
  • 1000 views
  • 6 likes
  • 5 in conversation