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

Hi SAS users-

 

I originally had a numeric variable, SubjectID, that had a length of 6 but the value displayed was up to 11 digits.  I accidentally thought this numeric variable was actually a character, so I converted it to numeric even though it was already numeric. (See SAS code below for my conversion step.)

 

This resulted in truncated values of the original variable, e.g. 20000000021 is now 20000000.  Is there anyway to retrieve the original value of SubjectID?  Or is it gone for good?

 

Thanks!

 

data convert;

set have (rename=(SubjectID=chrx_SubjectID));

 

length SubjectID 8.

SubjectID=input(chrx_SubjectID,8.);

run;

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

If you ran that code then your original value is in the variable CHRX_SUBJECTID.

What happens if you run this test:

data _null_;
  set convert obs=4 ;
  put SubjectID= chrx_SubjectID= ;
run;

View solution in original post

5 REPLIES 5
PaigeMiller
Diamond | Level 26

You should be able to go back to your original data source and retrieve the 11 digit number.

--
Paige Miller
sophia_SAS
Obsidian | Level 7

By my original data source, are you referring to the "Have" dataset?  If so, that dataset is no longer available. The only one I have access to is the 'convert' dataset. 

Tom
Super User Tom
Super User

If you ran that code then your original value is in the variable CHRX_SUBJECTID.

What happens if you run this test:

data _null_;
  set convert obs=4 ;
  put SubjectID= chrx_SubjectID= ;
run;
Astounding
PROC Star

That sounds like a pretty dangerous combination:

 

  • Make some changes to the data
  • Don't check to see whether the changes worked properly
  • Get rid of the original data

Hopefully you can learn from this for next time.

FreelanceReinh
Jade | Level 19

Hi @sophia_SAS,

 

As to your initial surprise about the length of SubjectID: The length of a numeric variable does not specify the maximum number of digits, but the number of bytes used to store the numeric values internally.

 

The length limits the precision to which numbers can be stored. In particular, there are "largest integers that can be safely stored in a given length": see the table in "Numerical Accuracy in SAS Software."

As you see in the table, on Windows and Unix systems length 6 (bytes) is sufficient for integers with 11 digits (and even for a limited range of 12-digit integers), whereas 5 bytes or less would be insufficient. An insufficient length means that certain integers beyond the limit are rounded to multiples of 2, 4, 8, 16, ... (powers of 2) and the effects on non-integer values are even more difficult to deal with.

 

Your program (but only with a semicolon after the LENGTH statement, rather than a period) has created the "truncated" versions of the original values in the following way:

  1. The numeric values in chrx_SubjectID were internally converted to character values (see the corresponding log message if you haven't discarded it) using the BEST12. format because the first argument of the INPUT function must be character.
  2. The resulting right-aligned (!) strings of length 12 were read with the 8. informat, which would read the leading blank plus the first seven digits of a 11-digit string. Hence, the result for 20000000021 would normally be 2000000, not 20000000.

To resolve the issue (as Tom has already mentioned), it should be no problem to drop the newly created variable SubjectID and rename chrx_SubjectID back to SubjectID:

data want;
set convert;
drop SubjectID;
rename chrx_SubjectID=SubjectID;
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
  • 5 replies
  • 3435 views
  • 2 likes
  • 5 in conversation