BookmarkSubscribeRSS Feed
venkatagopinath
Fluorite | Level 6

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

 

 

6 REPLIES 6
sbxkoenk
SAS Super FREQ
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 */
ChrisNZ
Tourmaline | Level 20

> Since i cant sort the data with this id variable because it is character.

 

That's an odd comment. Why can't you? 

SASKiwi
PROC Star

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?

ballardw
Super User

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".

Kurt_Bremser
Super User

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

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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
  • 6 replies
  • 933 views
  • 5 likes
  • 6 in conversation