BookmarkSubscribeRSS Feed
Smitha9
Fluorite | Level 6

Hi,

I have a number "20130101" which is character

 

I want this to change to numeric. same format.

 

thanks.

2 REPLIES 2
PeterClemmensen
Tourmaline | Level 20

Do this

 

data test;
   c = "20130101";
   d = input(c, yymmdd8.);
   format d yymmddn8.;
run;
Tom
Super User Tom
Super User

The word FORMAT in SAS has a specific meaning.  A SAS FORMAT is instructions for how to display values as text.  There are numeric formats that are used with numeric values and character formats that work with character values.

To go from text strings to values you use an INFORMAT.  A numeric informat will convert strings into numeric values and character informats convert strings into other strings.

 

So if you want convert string  "20130101" into a number you can use the normal numeric informat and you will get the value 20,130,101. You have that value displayed as the string "20130101" by using the 8. format.

 

Now SAS stores dates as the number of days since 1960.  So if you want to interpret the string "20130101" as meaning the first day of the year 2013 then use the YYMMDD8. informat.  That will generate a value of 19,359.  If you want to display that in a way that a human will recognize then use any of the valid date formats.  For example you could use the DATE9. format to have it displayed as "01JAN2013" or the YYMMDD10. format to have it displayed as "2013-01-01".  To have it display like the original string use the YYMMDDN8. format.

210   data test;
211     string='20130101';
212     number=input(string,32.);
213     date=input(string,yymmdd10.);
214     format date yymmddn8.;
215     put (_all_) (=);
216   run;

string=20130101 number=20130101 date=20130101
NOTE: The data set WORK.TEST has 1 observations and 3 variables.

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!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 390 views
  • 0 likes
  • 3 in conversation