04-11-2017 03:50 PM - edited 04-11-2017 03:51 PM
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...
select DATEFIELD, DATEFIELD format=MMDDYY10. AS col_a , PUT(DATEFIELD,8.) as col_b
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.
04-11-2017 05:06 PM
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.
04-11-2017 06:57 PM
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;
04-12-2017 09:06 AM
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.
04-12-2017 11:31 AM
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.
04-12-2017 12:47 PM
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.
Need further help from the community? Please ask a new question.