BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
me55
Quartz | Level 8

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. 

1 ACCEPTED SOLUTION

Accepted Solutions
Tosin
Calcite | Level 5

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

10 REPLIES 10
collinelliot
Barite | Level 11

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

ballardw
Super User

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

Tosin
Calcite | Level 5

Try this....

 

 

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

me55
Quartz | Level 8

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. 

ballardw
Super User

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;
me55
Quartz | Level 8

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. 

me55
Quartz | Level 8

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. 

Peter_C
Rhodochrosite | Level 12
Not just those two
All formats return character strings through a PUT() function
Peter_C
Rhodochrosite | Level 12
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
me55
Quartz | Level 8

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. 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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