DATA Step, Macro, Functions and more

create a substract ing variables

Occasional Learner
Posts: 1

create a substract ing variables

I'd like to create a variable called "Length of stay" by subtracting the separation date (sepdate) from the admit date (addate) in the hosp file.


addate= 2008-10--25



 I wrote :

data hosp; 

set hosp;

los= sepdate - addate;



but it does not work.

thank you

Valued Guide
Posts: 629

Re: create a substract ing variables

Subtracting  strings is not possible. You have to convert the strings to sas dates using input function. You should check why those "dates" have not been converted to sas dates during data import.


data coolDates;
   set have(rename=(appDate=strAppDate sepDate=strSepDate));

   length appDate sepDate 8;
   format appDate sepDate yymmdds10.;

   appDate = input(strAppDate, yymmdd10.);
   sepDate = input(strSepDate, yymmdd10.);

   /* Calculations ...*/

   drop strAppDate strSepDate;
Frequent Contributor
Posts: 112

Re: create a substract ing variables

Make sure sepdate and addate are both Numeric Data Type to have numeric operations.
If not Numeric then convert them to Numeric using informat.
Everything else looks ok.
Super User
Posts: 10,569

Re: create a substract ing variables

"does not work" is a VERY POOR description of your problem. Supply the log, and give a clear description how the result you got does not meet your expectations. Don't force us to pry important information out of your nose.

Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Ask a Question
Discussion stats
  • 3 replies
  • 4 in conversation