BookmarkSubscribeRSS Feed
jamie0111
Calcite | Level 5

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.

3 REPLIES 3
SASKiwi
PROC Star

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;
ballardw
Super User

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.

Tom
Super User Tom
Super User

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

 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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
  • 3 replies
  • 692 views
  • 0 likes
  • 4 in conversation