DATA Step, Macro, Functions and more

How can I add 65days to yymmdd column.

Reply
Occasional Contributor
Posts: 9

How can I add 65days to yymmdd column.

Hi there.

I have a column name 'checkoutday'

Each row has check out day information like 

    checkoutday

      20160818

      20160911

      20160730

             ...

 

And I want to add 65days to each row.

So I tried to add 65days to each row like

 

 

Data want ;

set From;

wantdate = checkoutday + 65;

run;

 

Data want;

set from;

wantdate = input(checkoutday, yymmdd8.) + 65;

format wantdate yymmd8. ;

run;

 

But it didn't work

Any tips or help will be much appreciated.

Thanks.

Super User
Posts: 3,108

Re: How can I add 65days to yymmdd column.

The solution depends on whether checkoutday is a numeric or character variable. If it is numeric this may work:

 

Data want;
set from;
wantdate = input(put(checkoutday, 8.), yymmdd8.) + 65;
format wantdate yymmdd8. ;
run;
Super User
Posts: 10,500

Re: How can I add 65days to yymmdd column.

[ Edited ]

Doesn't work is awful vague.

Are there errors in the log: Post the code and log in a code box opened with the {i} to maintain formatting of error messages.

No output? Post any log in a code box.

Unexpected output? Provide input data in the form of a dataset, the actual results and the expected results. Data should be in the form of a data step. Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the {i} icon or attached as text to show exactly what you have and that we can test code against.

 

However basic understanding needed: INPUT function really wants a character value. Since apparently the value you have is numeric then it needs to be converted to text with Put before using Input per @SASKiwi because SAS will often attempt to do what you want but the default conversion of numeric to text will almost certainly use a BEST format that is incorrect for what you are attempting.

Super User
Super User
Posts: 6,500

Re: How can I add 65days to yymmdd column.

If your current variable is numeric then you need to first convert it to string before trying to use INPUT() function on it. Otherwise SAS will convert it using the BEST12. format and so you will have four leading spaces that the informat YYMMDD8. will not understand. '    2016' is not a valid value for that informat.

 

Also did you want to put it back into that strange non-date numeric variable?

 

data have;
  input checkoutday @1 daychar $8. ;
cards;
20160818
20160911
20160730
;

data want ;
  set have ;
  wantdate2= input(put(checkoutday,z8.), yymmdd8.) + 65;
  wantdate3= input(daychar, yymmdd8.) + 65;
  format want: yymmdd10. ;
  wantnum = input(put(input(put(checkoutday,z8.),yymmdd8.)+65,yymmddn8.),8.);
run;
Obs    checkoutday    daychar      wantdate2     wantdate3     wantnum
 1       20160818     20160818    2016-10-22    2016-10-22    20161022
 2       20160911     20160911    2016-11-15    2016-11-15    20161115
 3       20160730     20160730    2016-10-03    2016-10-03    20161003

 

Ask a Question
Discussion stats
  • 3 replies
  • 121 views
  • 0 likes
  • 4 in conversation