SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
KRHE
Obsidian | Level 7

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                                               ."

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

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

View solution in original post

6 REPLIES 6
PaigeMiller
Diamond | Level 26

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
ballardw
Super User

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.

KRHE
Obsidian | Level 7
The issue was that i did not get any error messages. :/. But it is now solved , the main issue was to define the 'date' variable as Date through datepart function
PaigeMiller
Diamond | Level 26

@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
KRHE
Obsidian | Level 7
I tried different types and the one i showed was the closest to the solution. That is why i posted it. However i will bear this in mind next time.Thank you again.

sas-innovate-white.png

Join us for our biggest event of the year!

Four days of inspiring keynotes, product reveals, hands-on learning opportunities, deep-dive demos, and peer-led breakouts. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 1159 views
  • 3 likes
  • 3 in conversation