BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ckyborg4
Calcite | Level 5

I want to calculate the age of a customer but sas is giving me errors. Anyone know how to fix this?

 

My Code:

%let todaysDate = %sysfunc(today()) format date09.;

PROC SQL;
CREATE TABLE WORK.QUERY_FOR_TABLE AS
SELECT
t1.SubscriberKey,
Datepart(t1.CustDOB) format date09. AS DOB,
&todaysDate AS TodaysDate,
intck('year', DOB, TodaysDate) as Age
FROM
CASE.CUSTOMERS t1
;
QUIT;
RUN;

 

Error Log:

 

ERROR: Function INTCK requires a numeric expression as argument 2.
ERROR: Function INTCK requires a numeric expression as argument 3.
ERROR: The following columns were not found in the contributing tables: DOB, TodaysDate.
1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

intck() returns the number of interval boundaries. If the interval is year then the number of boundaries between 31Dec2020 and 01Jan2021 would be 1.

If you are really after the age then using the yrdif() function with basis 'age' is likely more what you're after.

%let todaysDate = %sysfunc(today());

PROC SQL;
  CREATE TABLE WORK.QUERY_FOR_TABLE AS
    SELECT 
      t1.SubscriberKey,
      Datepart(t1.CustDOB) format date09. AS DOB format=date9.,
      &todaysDate AS TodaysDate format=date9.,
      floor(yrdif(Datepart(t1.CustDOB), &todaysDate,'age')) as Age
    FROM
      CASE.CUSTOMERS t1
  ;
QUIT;

yrdif() returns the age with a decimal component. Use floor() - or int() - to only get the integer part of the age.

View solution in original post

5 REPLIES 5
ballardw
Super User

If you do

%put &todaysdate. ;

you can see that the value of &todaysdate is

    %put todaysdate is: &todaysdate.;
todaysdate is: 22379 format date09.

Which in not way resembles and number. And if you actually have the value formatted it is still not a number.

You want for this example:

%let todaysDate = %sysfunc(today());

PROC SQL;
CREATE TABLE WORK.QUERY_FOR_TABLE AS
SELECT
t1.SubscriberKey,
Datepart(t1.CustDOB) format date09. AS DOB,
&todaysDate AS TodaysDate,
intck('year', DOB, TodaysDate) as Age
FROM
HACKCASE.CUSTOMERSCOTIAREWARDS t1
;
QUIT;
RUN;

 

mkeintz
PROC Star
I believe you need "calculated dob" and "calculated todaysdate".
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
ckyborg4
Calcite | Level 5

I tried copy pasting the same code in, but it still gives me the same errors. I ran the code without the intck line and checked the column value type, they both indicated they were of type numeric. 

sbxkoenk
SAS Super FREQ

Hello,

 

this working code should get you where you want to be.

data abc;
 set sashelp.class;
 dob=INTNX('DAY',today(),-age*365);
 format dob ddmmyy10.;
run;

%let todaysDate = %sysfunc(today());

PROC SQL;
CREATE TABLE WORK.QUERY_FOR_TABLE AS
SELECT
t1.Name,
t1.dob format=date09. AS DOB,
&todaysDate. AS TodaysDate format=ddmmyy10.,
intck('year', DOB, &todaysDate.) as Age
FROM
work.abc t1
;
QUIT;
/* end of program */

Koen

Patrick
Opal | Level 21

intck() returns the number of interval boundaries. If the interval is year then the number of boundaries between 31Dec2020 and 01Jan2021 would be 1.

If you are really after the age then using the yrdif() function with basis 'age' is likely more what you're after.

%let todaysDate = %sysfunc(today());

PROC SQL;
  CREATE TABLE WORK.QUERY_FOR_TABLE AS
    SELECT 
      t1.SubscriberKey,
      Datepart(t1.CustDOB) format date09. AS DOB format=date9.,
      &todaysDate AS TodaysDate format=date9.,
      floor(yrdif(Datepart(t1.CustDOB), &todaysDate,'age')) as Age
    FROM
      CASE.CUSTOMERS t1
  ;
QUIT;

yrdif() returns the age with a decimal component. Use floor() - or int() - to only get the integer part of the age.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 5 replies
  • 1221 views
  • 1 like
  • 5 in conversation