BookmarkSubscribeRSS Feed
chelsealutz
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!

6 REPLIES 6
Reeza
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;

run;

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

 

chelsealutz
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?

Reeza
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;
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

Paul_OldenKamp_org
Obsidian | Level 7

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;

 

Reeza
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.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

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

View all other training opportunities.

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