turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- SAS Procedures
- /
- Calculate age at time of event

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

03-09-2016 10:32 AM

Good morning experts!

I'm wondering if the following scenario is even possible in SAS - trying desperately to avoid having to do it by hand...

I have 550 observations. Among my variables, I have the year the child was born, the month the child was born, the day the child was born (so, essentially, the birthdate split into three variables), child's age in months at the time of survey, and the date (year, month, day) that they recevied a vaccine. Is there a way to calculate how old in months each child was when they received their vaccine according to the record, or do I just need to go through all 550 and do that on my own?

Hoping someone has an idea! Thanks in advance!

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to chelsealutz

03-09-2016 10:37 AM

Use the MDY function to compose SAS date values from the year/month/day of the dates, and then use the INTCK function to calculate the difference in months.

---------------------------------------------------------------------------------------------

Maxims of Maximally Efficient SAS Programmers

Maxims of Maximally Efficient SAS Programmers

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to chelsealutz

03-09-2016 10:46 AM

Because "months" is actually an inconsistent term, I would suggest calculating the number of days and dividing by 365.25/12 = 30.4375.

To calculate the number of days you can subtract the two date variables.

```
Data Want;
Set Have;
*calculations here;
DOB = MDY(month, day, year);
Age_Days = Vaccine_Date - DOB;
Age_Months = Age_Days/30.4375;
run;
*display results;
proc print data=want(obs=20);
run;
```

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Reeza

03-09-2016 04:54 PM

But will I have to repeat that code for *every* 550 observations? It appears you have to put in the specific year, month, and day?

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to chelsealutz

03-09-2016 05:19 PM

No, you put in the variable names that represent the month, year and date, not the numeric values values.

SAS data step loops automatically, so it will execute the same calculation for each row in your data set.

Assume that you wanted to calculate BMI for example, looking at a sample data set SASHELP.CLASS, it would look like the following and work for every row. This code will run on your computer.

```
*View data set before calculation;
proc print data=sashelp.class;
run;
data want; *Create a new dataset called WANT;
set sashelp.class; *Identify the input data set;
BMI = (weight / (height**2)) * 703; *calculation of BMI;
format BMI 8.2; *Control presentation of BMI, display to 2 decimal places;
run;
*view data output;
proc print data=want;
run;
```

If you're new to SAS, just a guess , I would recommend the first SAS programming course which is available for free.

https://support.sas.com/edu/schedules.html?ctry=us&id=2588

Other training resources (youtube video's and blogs) can be found on the right hand side under resources here:

https://communities.sas.com/t5/SAS-Analytics-U/bd-p/sas_analytics_u

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to chelsealutz

03-09-2016 07:37 PM

Hi,

Some of the other answers provided a method for calculating an approximate age in months. In some situations the result will be in error.

Your vaccine study monitors will probably not like an approximate age.

Since we are programming computers it's possible to program an exact solution that is almost always correct. One will use SAS code like: age_months = floor((intck('month',birthdate,vacine_date)-(day(vacine_date) < day(birthdate))));

Here's a sample program showing how to use it:

```
data day_month_year; *** data step to get data into day_month_year SAS dataset;
*** there are several ways to do this input, this works in traditional SAS;
infile datalines;
input day month year birthdate date9.;
datalines
31 01 1998 01dec1990
02 08 2003 18jan1993
15 12 2001 15aug1991
... the rest of the 550 subjects
;;
run;
data ages_months; *** new dataset with calculated vars;
set day_month_year; *** read in dataset created above;
vacine_date = mdy(month,day,year); *** use mdy function to create SAS date variable;
age_months = floor((intck('month',birthdate,vacine_date)-(day(vacine_date) < day(birthdate))));
*** line above calculates age in months using floor, intck, and day functions;
run;
proc print
data = ages_months;
run;
```

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to chelsealutz

03-09-2016 08:01 PM

An accurate number of months won't be comparable from person to person so I'm not sure thats valid. On the other hand if the recommendations are by month then it makes sense.