Published on :

Sans titre

This code is also available in: Deutsch Español Français
Awaiting validation
1 Code Block
DATA STEP Data
Explanation :
This DATA step block creates a dataset named 'test'. It defines two variables, 'birthday' and 'date2', as dates. The `date9.` formats and `informat` are used for reading and displaying dates. The values are then read directly from the provided `datalines`, thus simulating an input dataset for age calculations. This data is internal to the script.
Copied!
1DATA test;
2 INPUT birthday date2;
3 informat birthday date2 date9.;
4 FORMAT birthday date2 date9.;
5 DATALINES;
6 16APR1986 16APR2017
7 15APR1986 16APR2017
8 17APR1986 16APR2017
9 29FEB2016 28FEB2017
10 29FEB2016 27FEB2017
11 29FEB2016 01MAR2017
12 ;
13RUN;
2 Code Block
PROC FCMP
Explanation :
This PROC FCMP (Function Compiler) defines a custom function named `AgeToday`. This function takes a birth date (`DateDay`) as an argument. It calculates the age in years by comparing the birth date with the current system date (`TODAY()`) using the `intck` function to count months. An adjustment is made for the day of the month (`day(bd)>day(to)`) to ensure correct age precision, handling cases where the anniversary month has passed but not yet the day.
Copied!
1PROC FCMP outlib=work.cat_function.test ;
2 function AgeToday(DateDay);
3 to = TODAY();
4 bd = DateDay;
5 age = int((intck('month',bd,to)-(day(bd)>day(to)))/12);
6 return(age);
7 endsub;
8RUN;
3 Code Block
PROC FCMP
Explanation :
This PROC FCMP defines a second custom function named `AgeDate`. Unlike `AgeToday`, this function is more generic and takes two arguments: a birth date (`Birthdate`) and a reference date (`Date`). It calculates the age in years between these two dates using logic identical to `AgeToday`, thus allowing the calculation of age for any specified date and not just the current date.
Copied!
1PROC FCMP outlib=work.cat_function.test ;
2 function AgeDate(Birthdate,Date);
3 to = Date;
4 bd = Birthdate;
5 age = int((intck('month',bd,to)-(day(bd)>day(to)))/12);
6 return(age);
7 endsub;
8RUN;
4 Code Block
OPTIONS
Explanation :
This 'options' statement is essential to make the FCMP functions defined in the `work.cat_function.test` catalog available for use in subsequent SAS steps, such as DATA steps.
Copied!
1options cmplib=work.cat_function;
5 Code Block
DATA STEP
Explanation :
This DATA step reads the previously created 'test' dataset (`set test;`). It calls the FCMP functions `AgeToday` and `AgeDate` to calculate the age relative to today (`AgeT`) and the age relative to the date specified in 'date2' (`AgeD`) respectively for each observation. The results are stored in new variables 'AgeT' and 'AgeD', which are added to the 'test' dataset.
Copied!
1DATA test;
2 SET test;
3 AgeT = AgeToday(birthday);
4 AgeD = AgeDate(birthday,date2);
5RUN;
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.
Copyright Info : Creation date : 16/04/2017 (fr) Last update : 16/04/2017 (fr) Author(s) : Nicolas DUPONT Tested on SAS Studio 9.4