- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
I have two date variables.
I've formatted the new_dateMcs, however i cannot seem to format the other one called date. Looking at the list of date formats (some 83 different formats) I cant seem to find one that corresponds to mine.
"data Judo2;
set Ealry_dog7_CL;
new_DateMcs = input(DateMCS,yymmdd10.);
format new_DateMcs yymmdd10.;
/* Calculate the difference in months*/
dif_months = intck('MONTHS', 'date', 'new_DateMcs');
run;"
"date new_DateMcs dif_months
19MAY2020:00:00:00.000 2022-03-01 .
19MAY2020:00:00:00.000 2022-03-01 .
19MAY2020:00:00:00.000 2022-03-01 ."
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
The variable named DATE is actually a date/time value not a date value. You have to convert it to a date value (by using the DATEPART function) to calculate the difference in months.
dif_months=intck('month',datepart(date),new_datemcs);
The INTCK function as an additional optional argument, which you can read about here and determines how the calculation of months is done.
By the way, despite your message subject referring to formatting, this is not a format issue. It works regardless of how the variables are formatted, or even if they are unformatted.
Paige Miller
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
The variable named DATE is actually a date/time value not a date value. You have to convert it to a date value (by using the DATEPART function) to calculate the difference in months.
dif_months=intck('month',datepart(date),new_datemcs);
The INTCK function as an additional optional argument, which you can read about here and determines how the calculation of months is done.
By the way, despite your message subject referring to formatting, this is not a format issue. It works regardless of how the variables are formatted, or even if they are unformatted.
Paige Miller
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
If that is close to the actual code you ran you generated invalid data messages because the syntax for INTCK is incorrect.
Please see:
1 data junk; 2 dif_months = intck('MONTHS', 'date', 'new_DateMcs'); 3 run; NOTE: Character values have been converted to numeric values at the places given by: (Line):(Column). 2:33 2:41 NOTE: Invalid numeric data, 'date' , at line 2 column 33. NOTE: Invalid numeric data, 'new_DateMcs' , at line 2 column 41. dif_months=. _ERROR_=1 _N_=1
The values of the dates in the intck function must be numeric. That means that quoted strings for the variable names is incorrect in addition to your Date variable apparently containing a datetime value.
Large economy sized hint: Any time you get unexpected results consult the Log. If you do not understand the log copy text from the log of the code and all notes or messages associated with the code. Then open a text box on the forum using the </> icon at the top of the message window and paste the text. The text box is important because it will preserve the formatting of any diagnostic messages that SAS provides and makes it easier to tell where your question ends and actual code/log begins.
Note: your data step starting with double quotes can't run either.
https://communities.sas.com/t5/SAS-Communities-Library/Working-with-Dates-and-Times-in-SAS-Tutorial/... has a PDF with much information about dates.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@KRHE wrote:
The issue was that i did not get any error messages. :/.
To tell you the truth, this means that you showed us code that was different than the code you actually ran. Please do not do this. From now on, show us the code you actually ran, or better yet the ENTIRE log for this DATA step or PROC, without any parts chopped out. We would appreciate it. And you will get better and faster answers.
Paige Miller
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content