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
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));
@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.
@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'.
@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.
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
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:
...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:
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;
@emaguin wrote:
... When I look at the viewtable for my file,..
VIEWTABLE will display the values using the attached format.
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.
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.
Ready to level-up your skills? Choose your own adventure.