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

Hi ,

our financial year is between 1st July to 30th jun. I have one  column  name date . I need to create the year column based the dates.

see example:

from 1st July 2018 to 30th Jun 2019 it is 2019

from 1st July 2019 to 30th Jun 2020 it is 2020

if we go to next year it should be 2021

 

dateyear
25/06/20192019
26/06/20192019
27/06/20192019
28/06/20192019
29/06/20192019
30/06/20192019
01/07/20192020
02/07/20192020
03/07/20192020
04/07/20192020
05/07/20192020
06/07/20192020
07/07/20192020
08/07/20192020
09/07/20192020
10/07/20192020
11/07/20192020
12/07/20192020
13/07/20192020
14/07/20192020
15/07/20192020
16/07/20192020
17/07/20192020
18/07/20192020
19/07/20192020
20/07/20192020

 

looking for code in proc sql.

 

thanks,

SS

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

Oops SQL

 

proc sql;
create table want as
select *,year(date)+(month(date)>6) as year
from have
order by date;
quit;

You need an order by in SQL though as the order is never guaranteed in SQL, so an extra overhead as opposed to datastep

View solution in original post

4 REPLIES 4
novinosrin
Tourmaline | Level 20
data have;
input date	:ddmmyy10.;
format date ddmmyy10.;
cards;
25/06/2019	2019
26/06/2019	2019
27/06/2019	2019
28/06/2019	2019
29/06/2019	2019
30/06/2019	2019
01/07/2019	2020
02/07/2019	2020
03/07/2019	2020
04/07/2019	2020
05/07/2019	2020
06/07/2019	2020
07/07/2019	2020
08/07/2019	2020
09/07/2019	2020
10/07/2019	2020
11/07/2019	2020
12/07/2019	2020
13/07/2019	2020
14/07/2019	2020
15/07/2019	2020
16/07/2019	2020
17/07/2019	2020
18/07/2019	2020
19/07/2019	2020
20/07/2019	2020
;

data want;
set have;
year=year(date)+(month(date)>6);
run;
novinosrin
Tourmaline | Level 20

Oops SQL

 

proc sql;
create table want as
select *,year(date)+(month(date)>6) as year
from have
order by date;
quit;

You need an order by in SQL though as the order is never guaranteed in SQL, so an extra overhead as opposed to datastep

ballardw
Super User

In SQL this should work if your date values are SAS dates.

(year(date) +(month(date) ge 7)) as FY

This works because the comparison will return a one when true when testing the month of the year for the date

Ksharp
Super User
data have;
input date	:ddmmyy10. year;
format date ddmmyy10.;
cards;
25/06/2019	2019
26/06/2019	2019
27/06/2019	2019
28/06/2019	2019
29/06/2019	2019
30/06/2019	2019
01/07/2019	2020
02/07/2019	2020
03/07/2019	2020
04/07/2019	2020
05/07/2019	2020
06/07/2019	2020
07/07/2019	2020
08/07/2019	2020
09/07/2019	2020
10/07/2019	2020
11/07/2019	2020
12/07/2019	2020
13/07/2019	2020
14/07/2019	2020
15/07/2019	2020
16/07/2019	2020
17/07/2019	2020
18/07/2019	2020
19/07/2019	2020
20/07/2019	2020
;

data want;
set have;
want_year=year(intnx('year.7',date,1));
run;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 2112 views
  • 0 likes
  • 4 in conversation