DATA Step, Macro, Functions and more

Numeric format to date format.

Accepted Solution Solved
Reply
Contributor
Posts: 23
Accepted Solution

Numeric format to date format.

Hi,
 
This is how the date variable in the dataset looks and it's in a numeric format.
svc_start_dt  svc_end_dt
08/09/01            04/09/03
10/30/2001      3/26/2002
 
I used this code to change the format and to create a duration variable.
 
data telecom_service;
set telecom;
st_dt = put(svc_start_dt,10.);
put st_dt = yymmdd10.;
end_dt=put(svc_end_dt,10.);
put end_dt = yymmdd10.;
duration = end_dt-st_dt;
run;
 
It is giving me a result without any error and has now changed into a character variable. Why does that happen? Also, now when it is a  character varaible, how did it calculate the difference between the dates?How can I change a variable from numeric to date?
 

Accepted Solutions
Solution
‎12-17-2016 08:22 AM
Super User
Posts: 19,820

Re: Numeric format to date format.

PUT converts the variable to a character variable. 

 

Your question is a bit unclear. I'm not sure what you're starting with and what you're trying to achieve in the end. SAS dates are counted as the number of days from Jan 1, 1960 and then formats are applied to show the date in the desired appearance. 

 

My my guess is although you say there's no error it's likely you have NOTES regarding a conversion of,type from character to numeric. SAS may have done the conversion for you when you did the subtraction. 

View solution in original post


All Replies
Solution
‎12-17-2016 08:22 AM
Super User
Posts: 19,820

Re: Numeric format to date format.

PUT converts the variable to a character variable. 

 

Your question is a bit unclear. I'm not sure what you're starting with and what you're trying to achieve in the end. SAS dates are counted as the number of days from Jan 1, 1960 and then formats are applied to show the date in the desired appearance. 

 

My my guess is although you say there's no error it's likely you have NOTES regarding a conversion of,type from character to numeric. SAS may have done the conversion for you when you did the subtraction. 

Super User
Posts: 7,809

Re: Numeric format to date format.

I just set up an example data set and ran your code:

data telecom;
input svc_start_dt :mmddyy10. svc_end_dt :mmddyy10.;
format svc_start_dt svc_end_dt mmddyy10.;
cards;
08/09/2001 04/09/2003
10/30/2001 03/26/2002
;
run;

data telecom_service;
set telecom;
st_dt = put(svc_start_dt,10.);
put st_dt= yymmdd10.;
end_dt = put(svc_end_dt,10.);
put end_dt= yymmdd10.;
duration = end_dt - st_dt;
run;

This is the log:

16         data telecom;
17         input svc_start_dt :mmddyy10. svc_end_dt :mmddyy10.;
18         format svc_start_dt svc_end_dt mmddyy10.;
19         cards;

NOTE: The data set WORK.TELECOM has 2 observations and 2 variables.
NOTE: DATA statement used (Total process time):
      real time           0.02 seconds
      cpu time            0.00 seconds
      
22         ;

23         run;
24         
25         data telecom_service;
26         set telecom;
27         st_dt = put(svc_start_dt,10.);
28         put st_dt= yymmdd10.;
                      _________
                      484
NOTE 484-185: Format $YYMMDD was not found or could not be loaded.

29         end_dt = put(svc_end_dt,10.);
30         put end_dt= yymmdd10.;
                       _________
                       484
NOTE 484-185: Format $YYMMDD was not found or could not be loaded.

31         duration = end_dt - st_dt;
32         run;

NOTE: Character values have been converted to numeric values at the places given by: (Line):(Column).
      31:12   31:21   
st_dt=15196
end_dt=15804
st_dt=15278
end_dt=15425
NOTE: There were 2 observations read from the data set WORK.TELECOM.
2                                                          The SAS System                            08:48 Monday, December 12, 2016

NOTE: The data set WORK.TELECOM_SERVICE has 2 observations and 5 variables.
NOTE: DATA statement used (Total process time):
      real time           0.10 seconds
      cpu time            0.01 seconds

You can clearly see the NOTE messages caused by trying to do date formatting on character variables. My Enterprise Guide interprets those messages already as errors (red "x" on the program icon).

Note that the result (duration) is numeric as intended, since it is created with a calculation.

You can completely omit the conversion, and simply do:

data telecom_service;
set telecom;
duration = svc_end_dt - svc_start_dt;
run;

which gives you the same result, but without problems in the log.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Posts: 5,511

Re: Numeric format to date format.

It's difficult to believe that your original dates are numeric, when they print in two different formats.  If they are numeric, you already have a simple formula for duration that you can apply, without any formats or transformations.  But I would double-check what is in those variables first by running a PROC CONTENTS.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 205 views
  • 0 likes
  • 4 in conversation