date to character format

Accepted Solution Solved
Reply
Contributor
Posts: 26
Accepted Solution

date to character format

[ Edited ]

okay, i am using sas eg and i have an input file.  the field i am importing is in YYMMDD format currently from a flat txt file.  so what i need to do is put this field into both a date format and a character formatted field (YYMMDD6.) but i cannot seem to get it to come out right.  i import it as a date field and then try...

 

proc sql;
select DATEFIELD, DATEFIELD format=MMDDYY10. AS col_a , PUT(DATEFIELD,8.) as col_b
    from DATATABLE;
;

 

however the output is...

 

DATEFIELD  col_a             col_b

170117         01/17/2017    20836

170201         02/01/2017    20851  ...

 

col b is left aligned like a text but it is clearly changing the original data to another, probably the translated date.  but i need it output 170117 as characters, as is with no translation. 


Accepted Solutions
Solution
‎04-12-2017 11:31 AM
Senior User
Posts: 1

Re: date to character format

Try this....

 

 

proc sql;
select DATEFIELD, DATEFIELD format=MMDDYY10. AS col_a , PUT(DATEFIELD,ddmmyyn6.) as col_b
    from DATATABLE;
;

View solution in original post


All Replies
Super Contributor
Posts: 284

Re: date to character format

The value 20836 is Jan 17, 2017, so my question is what the format is on DATEFIELD?

Grand Advisor
Posts: 10,210

Re: date to character format

This

PUT(DATEFIELD,8.) as col_b

tells SAS to display the value as 8 digits.

 

PUT(DATEFIELD,yymmdd6.) as col_b

would display as date in that format

Solution
‎04-12-2017 11:31 AM
Senior User
Posts: 1

Re: date to character format

Try this....

 

 

proc sql;
select DATEFIELD, DATEFIELD format=MMDDYY10. AS col_a , PUT(DATEFIELD,ddmmyyn6.) as col_b
    from DATATABLE;
;

Contributor
Posts: 26

Re: date to character format

yeah but i dont need a date in return.  i need col_b to output the date but in character format.  DATEFIELD is imported using YYMMDD6. format.  with that field i need it to populate two columns, one is a date field in a different format.  that is not an issue however the other column i need it to populate is a character field.  so it need to change format or maybe change the way it is imported i guess i am not sure...but i need that one column after imported to fill two columns, a date and a text column.  so far every way i have tried to convert that date to char ends up with a sas converted date i believe. 

Grand Advisor
Posts: 10,210

Re: date to character format

There are about 30 different SAS supplied date formats. What is your result supposed to look like?

 

Replace the yymmdd6. in the code below with the format you want though it really sounds like you want yymmdd6.

PUT(DATEFIELD,yymmdd6.) as col_b

would display 17Jan2017 as 170117

 

data _null_;

   x='17Jan2017'd;
   put x= yymmdd6.;
run;
Contributor
Posts: 26

Re: date to character format

yes, i can use put to make it look like i want it to but the problem is a date formatted data will not feed into a character formatted column.  so it puts me in the same spot which is virtually the same code i was running above. 

 

i will try it again just in case i did something wrong but they key is I NEED THE DATA TO FEED INTO A CHARACTER FORMATTED COLUMN and a date one.  i clearly already have the date part solved it is the character part i am having issues with. 

Contributor
Posts: 26

Re: date to character format

okay, so after researching a bit more, i found that YYMMDD10. and YYMMDD6. are in character format so those two would be able to go from a date field directly into a character field.  since i have them in ...8. format, i guess that is not character format?!  but alas, i got it working. 

 

thanks everyone for the help on this.  i appreciate it. 

Valued Guide
Posts: 2,174

Re: date to character format

Not just those two
All formats return character strings through a PUT() function
Valued Guide
Posts: 2,174

Re: date to character format

It is just that 8. returns the underlying number which is the way SAS stores numeric values. Only with a date format will you get that date string
Contributor
Posts: 26

Re: date to character format

that is the perfect response i was looking for.  i understand completely now how that works and why it was not working before.  so the function PUT converts the date to characters not that format as a character format.  so when i was importing it as YYMMDD6.,

 

it wouldnt go into the field because it was just a date and if i take even that and use PUT(DATEFIELD,YYMMDD6.) that would allow it to go into a character field. 

 

i really appreciate that repsonse peter_c.  now i really understand it. 

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 10 replies
  • 180 views
  • 0 likes
  • 5 in conversation