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!
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.
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;
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?
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
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;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.