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

Hi guys, 

I'm trying to convert a character variable to numeric. 

The variable represents IDs (i.e., anonymized patients) I received from an hospital.

While converting to numeric, the last value is trimmed. For example: 800501185 becomes 80050118. Is there a way to prevent the trimming? 

 

Here the code I'm using: 

 

data myDATA;
  set myDATA;
  numeric_var = input(Patients, 8.);
  put numeric_var=;
run;

Original variable "Patients": Char $11. 

 

Thank you in advance 

1 ACCEPTED SOLUTION

Accepted Solutions
yabwon
Onyx | Level 15
data myDATA;
  Patients='800501185';
  numeric_var = input(Patients, best32.);
  put numeric_var=;
run;
_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



View solution in original post

3 REPLIES 3
yabwon
Onyx | Level 15
data myDATA;
  Patients='800501185';
  numeric_var = input(Patients, best32.);
  put numeric_var=;
run;
_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



Kurt_Bremser
Super User

 

DON'T.

Patient ID's are codes, not numbers, and should always be stored as character. You will never do calculations with them, but numeric might lead to imprecisions or loss of information (like leading zeroes).

Astounding
PROC Star

Technically, your program instructs SAS to read 8 characters:

numeric_var = input(Patients, 8.);

That's what "8." means:  read 8 characters.  If you want SAS to read a different number of characters, that's the statement to change, perhaps to one of.

numeric_var = input(Patients, 9.);
numeric_var = input(Patients, 11.);

The solution from @yabwon will work.  But technically, "best32." is a format, not an informat.  So SAS just changes "best32." automatically to be "32." and gets the proper result.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

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