Turn on suggestions

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

Showing results for

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

Options

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Posted 03-09-2016 10:32 AM
(2550 views)

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!

6 REPLIES 6

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

*every* 550 observations? It appears you have to put in the specific year, month, and day?

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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.

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

**If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. **

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.