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 2025: Register Now

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!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 1266 views
  • 5 likes
  • 6 in conversation