Hi! I would like to seek your help to solve my question!
My existing variable format is Char "2018-08-31 00:00:00" and I want to convert it to SAS Date9. format. Please see my code as below:
Data Temp;
set T;
format AA date9.;
AA=input(char,MMDDYY10.);
run;
Really hope you can help me! Thanks a lot!
data want; char_date="2018-08-31 00:00:00"; num_date=input(substr(char_date,1,10),yymmdd10.); format num_date date9.; run;
data want; char_date="2018-08-31 00:00:00"; num_date=input(substr(char_date,1,10),yymmdd10.); format num_date date9.; run;
Actually, the informat "substrings" on its own:
data have;
input char :$20.;
cards;
2018-08-31 00:00:00
;
run;
data want;
set have;
format aa date9.;
aa = input(char,yymmdd10.);
run;
True, but you know my thoughts on implicit anything.
Here's an example of how to:
1. Read it in as date time
2. Extract date portion
3. Extract time portion
4. Keep as datetime and format as date to view
data have; x="2018-08-31 00:00:00"; *import and convert to date time variable; y_datetime=input(x, anydtdtm.); *get only the date part; y_date=datepart(y_datetime); *get only the time part; y_time=timepart(y_datetime); *keep as datetime and format as date; Y_formatted = Y_datetime; format y_datetime datetime. y_date date9. y_time time. y_formatted dtdate9.; run; proc print data=have;run;
SAS Output
Obs | x | y_datetime | y_date | y_time | Y_formatted |
---|---|---|---|---|---|
1 | 2018-08-31 00:00:00 | 31AUG18:00:00:00 | 31AUG2018 | 0:00:00 | 31AUG2018 |
@New_SAS_user76 wrote:
Hi! I would like to seek your help to solve my question!
My existing variable format is Char "2018-08-31 00:00:00" and I want to convert it to SAS Date9. format. Please see my code as below:
Data Temp;
set T;
format AA date9.;
AA=input(char,MMDDYY10.);
run;
Really hope you can help me! Thanks a lot!
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.