Discover More Tips and Techniques on This Blog

How to convert a character variable that represents a date into a SAS date

Convert a character variable that represents a date into a SAS date

Use the INPUT function to convert a character value that represents a date into a SAS date value.

Data one;
input chardate1 :$6. chardate2 :$9. chardate3 $10. chardate4 :$9.;
datalines;
010199
31dec1999
21/09/2005
5/9/2005; Run;

/* Use the INPUT function to convert a character value that represents a date
*//* into a SAS date value. Choose the second parameter to the INPUT function
*//* based upon what the current character value looks like. Use a FORMAT
*//* statement to apply the date format you want when you are done. *//*


*//* Note: If you are in SAS 9.0 or above, you may prefer using the ANYDTDTEw.
*//* Informat as the second argument to the INPUT function. ANYDTDTEw.
*//* can read multiple date layouts. Refer to the SAS Language Reference,
*//* Dictionary under INFORMATS for more information. */



data two;
set one;
sasdate1=input(chardate1,mmddyy6.);
sasdate2=input(chardate2,date9.);
sasdate3=input(chardate3,ddmmyy10.);
sasdate4=input(chardate4,ddmmyy10.);
format sasdate1 mmddyy10. sasdate2 yymmdd10. sasdate3 date9. sasdate4 monyy7. ;
run;

proc print;
run;


RESULTS:
Obs chardate1 chardate2 chardate3 chardate4 sasdate1 sasdate2 sasdate3 sasdate4 1
01 0199 31dec1999 21/09/2005 5/9/2005 01/01/1999 1999-12-31 21SEP2005 SEP2005
source: http://support.sas.com/kb/24/591.html

No comments:

Post a Comment

Disclosure:

In the spirit of transparency and innovation, I want to share that some of the content on this blog is generated with the assistance of ChatGPT, an AI language model developed by OpenAI. While I use this tool to help brainstorm ideas and draft content, every post is carefully reviewed, edited, and personalized by me to ensure it aligns with my voice, values, and the needs of my readers. My goal is to provide you with accurate, valuable, and engaging content, and I believe that using AI as a creative aid helps achieve that. If you have any questions or feedback about this approach, feel free to reach out. Your trust and satisfaction are my top priorities.