Help using Base SAS procedures

Display date in time format

Reply
Frequent Contributor
Posts: 141

Display date in time format

data uw_hist_1;

set uw_hist;

decision_date1=datepart(decision_date);

decision_date2=datepart(decision_date);

format decision_date1 mmddyy10.;

put decision_date2 hhmm8.2;

run;

 

LNKEY

DECISION_DATE

decision_date1

decision_date2

4001532474

24Sep2016 7:24:00.000

09/24/2016

20721

4001474487

29Sep2016 16:52:00.000

09/29/2016

20726

4001474487

27Oct2016 18:11:00.000

10/27/2016

20754

4001532474

28Nov2016 14:24:00.000

11/28/2016

20786

 

I want to display the time for decision_date2 in time format like this I used hhmm8.2 however I get different results in decision_date2.  How can I obtain the time format and would it maintain once I exported, especially in ODS whcih is the ultimate output

Super User
Posts: 11,343

Re: Display date in time format

[ Edited ]

By any chance do you want the TIMEPART of decision_date for the second value?

 

decision_date2=TIMEpart(decision_date);

Times and Datetimes in SAS are seconds, Dates are days. You will not have much luck displaying DATES with a TIME value.

 

If you permanently attach a format to a variable that will generally be the default for most output that does not involve a calculation (sas as mean in proc means/summary output for instance). Or specify the format in procedure generating the output such as Proc print. One of the nice things with the SAS formats is the ability to override them as needed.

 

 

By the way, one of my pet peeves is calling datetime values a date, or time values a date or dates a time/datetime. While other data systems may not care the measurement differences in SAS make it important.  Proper use of functions and or formats requires that understanding. Both values are numeric and numeric functions generally don't care what you give them. Consider:

 

data _null_;
   file print;
   x="10:15:23"t;
   y=month(x);
   put y=  ;
run;

I declared a TIME variable and them applied a DATE function. What would the result for Y actually mean if it appears in your output?

 

Trusted Advisor
Posts: 1,560

Re: Display date in time format

Your code:

data uw_hist_1;
set uw_hist;
decision_date1=datepart(decision_date);
decision_date2=datepart(decision_date);  /* Didn'y you mean: timepart ? */
format decision_date1 mmddyy10.;
put decision_date2 hhmm8.2;
run;

What is the logic of subsetin datepart and displaying it with hhmm format ?

 

Ask a Question
Discussion stats
  • 2 replies
  • 141 views
  • 0 likes
  • 3 in conversation