Hi,
I have data where the ID of the people are in character format and the data looks like this
ID
840-02-001
840-02-002
840-02-003
Can you please suggest how to convert this text to numeric so that i can sort and also convert back to same character text. Since i cant sort the data with this id variable because it is character.
data id;
set name;
ptid =input(id,10.);
run;
Help to correct the code for reading character with Hyphen and converting to numeric for sorting.
Thanks
data have;
LENGTH ID $ 10;
input ID $ ;
cards;
840-02-001
840-02-002
840-02-003
;
run;
data want;
set have;
IDnum=input(compress(ID,'-'),10.);
run;
/* end of program */
> Since i cant sort the data with this id variable because it is character.
That's an odd comment. Why can't you?
Based on the data you have supplied the sort order will be the same if you compare a character sort with a numeric sort. Why bother converting?
Why? Where are you going to perform arithmetic with ID values?
Your example doesn't show any likely problem with sorting, so provide a more complex example that did not sort the way you expect it to.
Proc sort has options like Sortseq with LInguistic and Numeric_Collation that allows you to override or specify some additional behaviors such as the numeric value instead of the default character order sort, ie "8 Main St" can come before "45 Main St".
There's no problem sorting character values:
data have;
length ID $ 10;
input ID;
cards;
840-02-001
840-02-002
840-02-003
;
proc sort data=have;
by id;
run;
Log:
69 data have; 70 length ID $ 10; 71 input ID; 72 cards; NOTE: The data set WORK.HAVE has 3 observations and 1 variables. NOTE: Verwendet wurde: DATA statement - (Gesamtverarbeitungszeit): real time 0.00 seconds user cpu time 0.01 seconds 76 ; 77 78 proc sort data=have; 79 by id; 80 run; NOTE: There were 3 observations read from the data set WORK.HAVE. NOTE: The data set WORK.HAVE has 3 observations and 1 variables. NOTE: Verwendet wurde: PROZEDUR SORT - (Gesamtverarbeitungszeit): real time 0.00 seconds user cpu time 0.00 seconds
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.