BookmarkSubscribeRSS Feed
Fluorite | Level 6

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!

Super User

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;


*display results;
proc print data=want(obs=20); 


Fluorite | Level 6

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?

Super User

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;

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;


*view data output;
proc print data=want;



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


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

Obsidian | Level 7



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.;
31 01 1998 01dec1990
02 08 2003 18jan1993
15 12 2001 15aug1991
... the rest of the 550 subjects

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;

proc print
   data = ages_months;


Super User
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. 

Register now!

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.

Get the $99 certification deal.jpg



Back in the Classroom!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 1 like
  • 4 in conversation