i am using infile to input a variable called "days". This variable has 6 observations (6,10,2,1,5,8). These values are number of days. Now I have a starting date i.e 22 Dec 1992. I want to create a new variable called "new_dates" in which the first value should be "22 dec 1992" and then incremented by each number of days (6,10,2,1,5,8). I know about INTNX but that increments a constant value to a given date. Couldnt figure out how to deal with this.
Need some help.
You will want to RETAIN the computed date as you add more days to the date.
INTNX function accepts numeric expressions for increment argument, so your are not limited to constants.
data increments;
input days @@;
datalines;
6 10 2 1 5 8
;
data want;
retain new_date '22dec1992'd;
set increments;
if _n_ = 1 then output;
new_date = intnx('day', new_date, days); * increment by the value of days variable;
output;
run;
Add your values to the date literal '22dec1992'd, as in:
data have;
do days=6,10,2,1,5,8;
output;
end;
run;
data want;
set have;
new_date=intnx('day','22dec1992'd,days);
format new_date date9.;
put days= new_date=;
run;
I inserted the PUT statement just so you can look at the log for the results, rather than opening dataset WANT to confirm.
Or, to use your INFILE approach:
data want;
infile datalines;
input days;
new_date=intnx('day','22dec1992'd,days);
format new_date date9.;
put days= new_date=;
datalines;
6
10
2
1
5
8
run;
thanks for your reply. whats the pupose of this line. output is same with or without it
put days= new_date=;
also its adding days to given date in each row. but its not incrementing on the next dates.
6 days + 22 dec 1992 = 28 dec 1992
but then next date should be
10 days + 28 dec 1992 = 7 jan 1993
@hastm wrote:
thanks for your reply. whats the pupose of this line. output is same with or without it
put days= new_date=;
I explained that in my note - please re-read.
also its adding days to given date in each row. but its not incrementing on the next dates.6 days + 22 dec 1992 = 28 dec 1992
but then next date should be
10 days + 28 dec 1992 = 7 jan 1993
And I should have re-read your initial question. But @RichardDeVen has addressed it.
thanks @mkeintz @RichardDeVen
You will want to RETAIN the computed date as you add more days to the date.
INTNX function accepts numeric expressions for increment argument, so your are not limited to constants.
data increments;
input days @@;
datalines;
6 10 2 1 5 8
;
data want;
retain new_date '22dec1992'd;
set increments;
if _n_ = 1 then output;
new_date = intnx('day', new_date, days); * increment by the value of days variable;
output;
run;
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!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.