- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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 */
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
> Since i cant sort the data with this id variable because it is character.
That's an odd comment. Why can't you?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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".
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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