I have a data set that looks something like this:
Name birth birth_day death death_day
Don 1913 02-Mar 1998 01-Nov
Jack 1924 04-Dec 1970 13-Sep
I want to calculate the age of the people but I am confused as to how to format the entries in birth_day and death_day numerically so that I can subtract their birthdays from their death days to calculate their age. My professor suggested using the dmy function, so I did
x=dmy(day(birth_day), month(birth_day), birth);
However that did not work as planned. How can I go about resolving this problem?
In response to rambles:
data _null_;
set mydata;
death_date = input(cats(death_day,'-', death), date11.);
birth_date = input(cats(birth_day,'-', birth), date11.);
format death_date birth_date date11. age 4.1;
age = yrdif(birth_date, death_date,'AGE'); ** The 'AGE' part only works in SAS 9.3+!!;
put death_date= birth_date= age=;
run;
I tried something like this, however I ge an error at the line format death_date birth_date date11. age 4.1; saying ERROR 48-59: The format $DATE was not found or could not be loaded.
The following example mimics your first row:-
data example;
Name = "Don";
birth_day = "02-Mar";
birth = "1913";
death_day = "01-Nov";
death = "1998";
run;
data example_with_age;
set example;
length death_date birth_date 5 age 3;
format death_date birth_date date11. age 4.1;
death_date = input(cats(death_day,'-', death), date11.);
birth_date = input(cats(birth_day,'-', birth), date11.);
age = yrdif(birth_date, death_date,'AGE'); ** The 'AGE' part only works in SAS 9.3+!!;
run;
I've assumed your birth and death variables are character. If they're numeric, you should surround them with a put statement in the input rows.
I've been lazy with calcalating age by using a feature in newer versions of SAS - it's a surprising hassle calculating age when using full dates. If you're using an older SAS version, let me know in a comment and I'll add the older method of calculating the age.
Related
Trying to display names and dates of Iowa ships that have had least one battle in the year 1943 or later. However, every time I go to use date under the Battles table I can not compare it to a date ('YYYY-DD-MM').
This is what it keeps showing
You had to cast your string representation of date as actual date data type:
(π name σ class = 'Iowa' (Ships)) ⨝ (π date σ date > date('1943-01-01') (Battles))
How Can I display Month Name Instead of Month Number (Not Data Format Number) In Crystal Report?
The MonthName function can be used to display the name of the month, when you provide a number between 1 and 12 (1 being January). It is useful for showing the month name in Group titles or labeling groups in charts.
It can be combined with the DatePart function to return the month name of a variable or calculation.
Syntax
MonthName(month, abbr)
Month A number from 1 to 12.
abbr Optional.A Boolean value. If true, the month name is abbreviated. The default is false.
Examples
Example Result
MonthName(5) “May”
MonthName(10) “October”
MonthName(10,True) “Oct”
MonthName(DatePart(“m”, CurrentDate)) “October” when the current date is 10/5/10.
I am stumped. I have a Timesheet class that holds work days in a dictionary called self.timesheet with dates as the keys and hours, rate as values. I am trying to write a function that can show all the entries in a user defined range of dates.
for now lets assume the key dates are simple integers 20 - 25. i tried this and it didn't do anything at all. no errors, just nothing.
def show_days(self):
date_from = input("From date: ")
date_to = input("To date: ")
t = self.timesheet
for dates in t[date]:
range(date_from, date to)
print(dates)
I can see this doesn't look right , I feel I need *for dates in range(date_from, date_to)* but I can't figure how to get it to loop over the dictionary keys like that.
You need to loop over the range, then check if that key is in the dictionary:
for day in range(date_from, date_to + 1):
if day in t:
print day, t[day]
Note that the values produced by range() do not include the end point, so I used date_to + 1 to ensure it is included anyway.
If your keys are not integers but, say, datetime.date objects, you'll have to construct some kind of loop with datetime.timedelta() to iterate over all dates between two values:
date = date_from = datetime.date(2012, 1, 15)
date_to = datetime.date(2012, 3, 12)
while date <= date_to:
if date in t:
print date, t[date]
date += datetime.timedelta(days=1)
If I want to find the maximum value of a column from two states aggregated by a member's ID, should this work?
=Aggr(
MaxString(
Aggr(NODISTINCT MinString({[State1]}DATE_STRING),MBR_ID)
+
Aggr(NODISTINCT MinString({[State2]}DATE_STRING),MBR_ID)
) , MBR_ID)
So if I had this data:
MBR ID DATE_STRING
1 20120101
1 20120102
1 20120103
And State1 had 20120101 selected and State2 has 20120103 selected, my expression would return 20120103 for member 1.
Thanks!
Edit: In SQL, this would look like:
WITH MinInfo (DATE_STRING, MBR_ID)
AS (SELECT MIN(DATE_STRING), MBR_ID FROM Table WHERE TYPE IN ('State1', 'State2') GROUP BY MBR_ID, TYPE)
SELECT MAX(DATE_STRING) DATE_STRING, MBR_ID FROM MinInfo GROUP BY MBR_ID
It would be easier to accomplish your goal if you convert your that to an actual date field
Assuming that you are using a chart where MBR_ID is the Dimension, if you want the maximum date (latest date) you can do the following:
=nummax(Max({[State1]}DATE_STRING),Max({[State2]}DATE_STRING))
To convert to a date, you can use this function:
date#(DATE_STRING,'[text format of the date]')
(The date format looks like YYYYMMDD to me, but if its day then month, you would use YYYYDDMM)
I'd suggest you format it in the script, so that you wont have to worry about it every time you need to use that date.
Date column is created using colmodel below.
This column shows values like 0101.0101.7070 for every date column.
if formatter:date is removed, date is correct.
How to show normal date values with formatter:date ?
{ "name":"Date",
"formatter":"date",
"datefmt":"dd.mm.yyyy",
"formatoptions":{"newformat":"dd.mm.yyyy"},
"editable":true
}
Update.
Data is passed from server using json in same dd.mm.yyyy format like
{"total":2,"page":1,"records":57,"rows":
[{"id":"9279","cell":["9279","42","","10.08.2011","","","","False"]},
{"id":"9278","cell":["9278","41","","12.08.2011","","","","False"]},
...
Using d.m.y formats in column options as suggested shows proper dates but with 2 year digits only..
I'm looking for a 4-digit year numbers. I tried d.m.yyyy format but this shows 8 digit year numbers and 1 for month and day as 01.01.70707070
I also tried to add srcformat: 'dd.mm.yyyy' to formatoptions but this does not change the wrong result.
To display the day as the number you should use j or d. For the displaying month as the number you should use n or m. The d and m includes 0 padding at the beginning if needed. The 'y' means two digit year format and Y means four digit year.
So you probably need srcformat: 'd.m.Y' or srcformat: 'j.n.Y'.
Use d.m.y instead of dd.mm.yy ,