BookmarkSubscribeRSS Feed
emaguin
Quartz | Level 8

Consider: teenage=(interviewdate - dob).

interviewdate is datetime16; dob is MMDDYY10

specific numbers: (18DEC23:18:48:18 - 06/08/2011) = 51.43...

so why not

teen_age=yrdif(datepart(p_abday_t1),p_abday_t1);

or
x1=datepart(p_abday_t1);
teen_age=yrdif(x1,p_abday_t1);

I'm doing something wrong because i don't understand something.

How do i do what i want to do?

Thanks, Gene Maguin

16 REPLIES 16
russt_sas
SAS Employee

I tired the following and it worked for me unless I am misunderstanding what you are asking:

p_abday_t1=input('18DEC23:18:48:18',datetime.);
dob=input('6/08/2011',mmddyy6.);
teen_age=yrdif(dob,datepart(p_abday_t1));

Tom
Super User Tom
Super User

@russt_sas wrote:

I tired the following and it worked for me unless I am misunderstanding what you are asking:

p_abday_t1=input('18DEC23:18:48:18',datetime.);
dob=input('6/08/2011',mmddyy6.);
teen_age=yrdif(dob,datepart(p_abday_t1));


Asking the MMDDYY informat to convert just the first 6 bytes of '6/08/2011' into will yield the date 08JUN2002.

Tom
Super User Tom
Super User

@russt_sas wrote:
mmddyy9. not 6.

Remember the INPUT() function does not care if the WIDTH you use on the INFORMAT specification is larger then the LENGTH of the string you are reading.  The maximum width that the MMDDYY informat supports is 10 so use that and avoid issue with values like '06/08/2011'.

Tom
Super User Tom
Super User

@russt_sas wrote:

I tired the following and it worked for me unless I am misunderstanding what you are asking:

p_abday_t1=input('18DEC23:18:48:18',datetime.);
dob=input('6/08/2011',mmddyy6.);
teen_age=yrdif(dob,datepart(p_abday_t1));


That one is also confused.  You should probably get an missing value.  And a note that '6/08/2011' could NOT be converted into a number.

Tom
Super User Tom
Super User

Not sure what your question is. Remember datetime values are stored in SECONDS and date values are in DAYS.  Also the format attached to a variable just impacts how it is displayed.  It does not change the value it contains.

 

So assuming you are talking about dates in this century (make sure the width on your DATETIME format specification is at least 19 so it will include the century) perhaps you want something like this?

 

Example:

 73         data test;
 74           interviewdatetime = '18DEC2023:18:48:18'dt ;
 75           dob = '08JUN2011'd ;
 76           days = datepart(interviewdatetime)-dob;
 77           years = yrdif(dob,datepart(interviewdatetime));
 78           format interviewdatetime datetime19. dob date9.;
 79           put (_all_) (=/);
 80         run;
 
 interviewdatetime=18DEC2023:18:48:18
 dob=08JUN2011
 days=4576
 years=12.528767123
quickbluefish
Barite | Level 11

As @Tom says, SAS just sees these as integers even though they may be formatted (basically, "masked") to look like dates or datetimes for convenience.  The way they look has no impact on calculations done with them. 

data test;
n1=14748;
n2=14748;
format n1 date9.;
format n2 datetime.;
run;

proc print data=test; run;

...results in:

quickbluefish_0-1738025403871.png

...May 18th 2000 is 14,748 days after Jan 1st, 1960.

...and 4:05:48AM on Jan 1st 1960 is 14,748 *seconds* after midnight on Jan 1st 1960.  

 

The datepart() function is literally just taking whatever number you give it and dividing it by 86,400 (the number of seconds in a day).  

data test;
n1=1975252985;
n2=datepart(n1);
n3=n1/86400;
format n2 n3 date9.;
run;

proc print data=test; var n2 n3; run;

result: 

quickbluefish_1-1738026121529.png

 

 

 

emaguin
Quartz | Level 8
Tom and all who replied. Thank you.



I apologize to you for describing the problem in a way that mislead you. So. Starting over. I've read in a data file and in the data file are the two named variables, interviewdatetime that is a date time variable and an actual value is 18DEC2023:18:48:18, and dob that is a date variable and an actual value is 08JUN2011. I understand the point about date-time variables being in second since the time origin, spss does the same thing. And that date variables are days since the date origin, excel does that.



It seemed to me that



teen_age=yrdif(datepart(p_abday_t1),p_abday_t1);



should work but the result is 51.43. From the two data values you can see the value is about 12.5 years.



It may be that the variable order is reversed but if so the difference would be negative-easily fixed.

I understand computation that has to be done.



I don't know how to write the syntax to do the required computations.


Thanks, Gene Maguin

PaigeMiller
Diamond | Level 26

This works for me, the YRDIF function gives a number around 12.5.

 

data a;
    interviewdt=dhms('18dec2023'd,18,48,18);
    dob='08JUN2011'd;
    teen_age=yrdif(dob,datepart(interviewdt));
run;        
--
Paige Miller
emaguin
Quartz | Level 8
Hi,
I see what you did and I can repeat it. So this means I have a deeper problem. When I look at the viewtable for my file, I see these exact characters: 06/08/2011 for dob and these exact characters: 18DEC23:18:48:18 for interviewdate. When I look at the viewtable after running the syntax you posted I see these exact characters: 18786 for dob and 2018544498. In my data file dob has a format of MMDDYY10 and interviewdate has a format of datetime16. In your file both are Number.
Ok. Is there a way to convert MMDDYY10 to number? Is there a way to convert datetime16 to number?
Maybe relevant. The dataset is exported from Redcap as a csv file and then read into excel and saved as an excel data file (xlsx) and that xlsx file is imported into sas through proc import (dbms=xlsx).
Thanks.
Tom
Super User Tom
Super User

@emaguin wrote:
... When I look at the viewtable for my file,..

VIEWTABLE will display the values using the attached format.

PaigeMiller
Diamond | Level 26

When I look at the viewtable for my file, I see these exact characters: 06/08/2011 for dob and these exact characters: 18DEC23:18:48:18 for interviewdate. 

 

What you see is either numeric variables formatted to 06/08/2011 or to 18DEC23:18:48:18; or these are character variables. We need to know which these are.

 

When I look at the viewtable after running the syntax you posted I see these exact characters: 18786 for dob and 2018544498.

 

These are numeric and unformatted. They are the dates and times that I asked for, unformatted. If you want to see what date and time they represent, you apply formats to them, you will see they are the exact dates and times that I asked for.

 

Maybe relevant. The dataset is exported from Redcap as a csv file and then read into excel and saved as an excel data file (xlsx) and that xlsx file is imported into sas through proc import (dbms=xlsx).

 

I don't think this is relevant. The relevant information we need is what is in your SAS data set. Please, we've gone long enough, please provide the actual SAS data set, using these instructions. Do not provide data in any other format — Excel is not acceptable, copy and paste from Excel is not acceptable.

--
Paige Miller
quickbluefish
Barite | Level 11
I'm not sure if this is just a typo in your message, but this:
teen_age=yrdif(datepart(p_abday_t1),p_abday_t1);
is using the same variable twice.

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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
  • 16 replies
  • 1344 views
  • 3 likes
  • 5 in conversation