Help using Base SAS procedures

AGE in Years and Months

Reply
Contributor
Posts: 34

AGE in Years and Months

Hi

I have data like below:

HAVE:

patid     ageyears     agemon     visdate

001          7               10               2014-02-10

003          15               6               2014-01-15

NEEDED like below:

patid     age                 visdate

001        P7Y10M          2014-02-10

003        P15Y6M          2014-01-15

Can Guru's help me with populating age as above

Thanks in advance

Regular Contributor
Posts: 233

Re: AGE in Years and Months

data want(keep=patid age visdate);
RETAIN patid age visdate;
set have;

age=cat('P',ageyears,'Y',agemon,'M');
RUN;

Capture.JPG

Contributor
Posts: 34

Re: AGE in Years and Months

Thanks Hima,

Instead of cat is there any other way we can do?

Thanks again

Regular Contributor
Posts: 233

Re: AGE in Years and Months

Another way that I know,

data want(keep=patid age visdate);

RETAIN patid age visdate;

set have;

age='P'||TRIM(LEFT(ageyears))||'Y'||TRIM(LEFT(agemon))||'M';

RUN;

Contributor
Posts: 34

Re: AGE in Years and Months

Thanks Again Hima.

Is there another way using ISO8601 (duration) formats.

Thanks

Regular Contributor
Posts: 233

Re: AGE in Years and Months

From the link below, I dont see any such format.

Dates, Times, and Intervals : SAS Date, Time, and Datetime Values

Contributor dkb
Contributor
Posts: 53

Re: AGE in Years and Months

ISO 8601 formats are available in v9 and are documented here:

SAS(R) 9.2 Language Reference: Dictionary, Fourth Edition

But personally I find them rather fiddly and I think Hima's suggestions are good.

Trusted Advisor
Posts: 3,214

Re: AGE in Years and Months

Using intnx interval functions should do the job

---->-- ja karman --<-----
Trusted Advisor
Posts: 3,214

Re: AGE in Years and Months

As everyone can work with his own calendar and clock there are many possibilities. But wanting to understand each other is needing an agreement that we use some standard.

For clock/time we have gmt hours/seconds being defined. For dates the iso-standards (8601) are becoming getting accepted. The metric system still being difficult for acceptance Metric system - Wikipedia, the free encyclopedia.  We could have done for the French revolutions way French Republican Calendar - Wikipedia, the free encyclopedia. Little chance for those now.  Date/time calculations are not that easy in a Georgian approach there is a 28-year period. Week-numbers are overlapping in the year-change.

The data/time convention with SAS as a number since the arbitrary point of zero 1 jan 1960 0:0:0 is a good way for calculations.

For date/time duration calculations intck: SAS(R) 9.3 Functions and CALL Routines: Reference

For advancing back and up in time intnx: SAS(R) 9.3 Functions and CALL Routines: Reference  

For presenting something age in a structure PyyMdd use formats. In this case the string is varying between 4 and 6. (advice fix that on 6, usage of blanks or zeros)

Of course you can manipulate strings as you did with pencil and paper. At some moment you will find that getting very complicated when using computers.         
Suppose you are needing to recalculate after some time, knowing the birthday or making a good guess of that. It is about data quality.

---->-- ja karman --<-----
Ask a Question
Discussion stats
  • 8 replies
  • 312 views
  • 1 like
  • 4 in conversation