To convert character to number, we use the input function. To convert number to character, we use the put function. At the beginning, we always get confuse which one is which. We can use the following way to help us remember:
When we write something on paper, everything is character. So if we try to convert something to character, we "put" the information in paper. If we try to get a number from paper (character), we "input"(read) the character to our brain (number).
char to num, and num to char example:
data eb;
chra="321.32";
chrb="5.2E6";
num=235;
chra2num=input(chra,best32.);
chrb2num=input(chrb, best32.);
num2chr=compress(put(num,best32.),' ');
run;
The best32. format in the above code is just an example, you can specific other formats.
char to date, and date to char example:
data eb2;
input date_char $10. mon day year;/* convert character to date */
date1=input(compress(date_char), anydtdte.);
date2=mdy(mon,day,year);
format date1 mmddyy10. date2 yymmdd10.;
cards;
11/ 1/1999 11 01 1999
1Sep1999 1 11 1999
12/24/2003 12 24 2003
7/ 1/2001 7 1 2001
;;
run;
If you check the result data set eb2, you will find that the third line (line of 12/24/2003) has a missing value.
The reason for the missing value is that the "anydtdate" format by default is equivalent "anydtdte9.", 9 digits. To correct this we use "anydtdte10." and now the third line convert to the right date.
No comments:
Post a Comment