How to convert char to numeric

Reply
N/A
Posts: 0

How to convert char to numeric

Hi friends,
I was trying PATIENT=COMPRESS('0'||TRIM(PATID));so it convert character

patid

123
234
534
334
232
122


but i want every patid include with zero and it must be numericplease send me immediately


Thanku advance
Super Contributor
Posts: 260

Re: How to convert char to numeric

Hello.

You can try PATIENT = PUT(PATID,Z4.) ;
Format Zx. displays integers with leading zeros on x digits.

Good luck.
Olivier
SAS Super FREQ
Posts: 8,643

Re: How to convert char to numeric

Hi!
Remember that internally, SAS will NOT store a leading zero as part of a numeric variable, so you only need to use the z4. format for display of the leading zero. So in my code below, you can display the numeric patient id either with the best4. format (no leading zeros) or with the z4. format (display leading zero). To turn a CHARACTER patient ID into a NUMERIC patient ID, you need to use the INPUT function.

To "prepend" a zero onto a character variable, you could use the technique that you originally showed, but your patient id will remain character.

IF patient ID is already NUMERIC, then you can turn it into a CHARACTER string with a leading zero using the PUT function.

But it sounded to me like your patient ID was already character and you wanted it to be numeric and that will take the INPUT function. But, that also requires that you use the Z4. format in order to display patient id once it is a number. See the attached code.

cynthia
[pre]
*** the code;
data testit;
input charpat $ numpat;
** convert charpat to new numeric var;
newnumpat1 = input(charpat,4.);

** convert numpat to new character var;
newcharpat1 = put(numpat,z4.);

** use concat to put zero onto char patient id;
** but patient id remains character;
newcharpat2 = '0'||trim(charpat);

format numpat best4. newnumpat1 z4.;
return;
datalines;
123 123
234 234
534 534
334 334
232 232
122 122
;
run;

proc print data=testit;
run;
[/pre]
Post a Question
Discussion Stats
  • 2 replies
  • 1041 views
  • 0 likes
  • 3 in conversation