BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
mssh2712
Calcite | Level 5
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?
 
1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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

3 REPLIES 3
Reeza
Super User

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. 

Kurt_Bremser
Super User

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.

Astounding
PROC Star

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.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 3 replies
  • 1207 views
  • 0 likes
  • 4 in conversation