BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
hastm
Fluorite | Level 6

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.

1 ACCEPTED SOLUTION

Accepted Solutions
RichardDeVen
Barite | Level 11

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;

 

View solution in original post

5 REPLIES 5
mkeintz
PROC Star

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;
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
hastm
Fluorite | Level 6

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

 
mkeintz
PROC Star

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

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
RichardDeVen
Barite | Level 11

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;

 

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!

How to Concatenate Values

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.

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
  • 5 replies
  • 1058 views
  • 2 likes
  • 3 in conversation