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

Any one can help?

I want to convert a datetime to a year using proc sql in sas Eg.

for example, convert any time between '01Apr2001:00:00:00'dt and '31Mar2002:00:00:00'dt  into 2002

                                          between  '01Apr2002:00:00:00'dt and '31Mar2003:00:00:00'dt into 2003

                                                  ............

                                                            '


1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

year=(year(datepart(date))+1)-(month(datepart(date))<=3);

*example for dates, not date time;

data have;

    do i='01Jan2012'd to '31Dec2013'd;

        year=(year(i)+1)-(month(i)<=3);

        output;

    end;

    format i date9.;

run;

View solution in original post

8 REPLIES 8
Reeza
Super User

Assuming its Proc SQL and not SQL Pass-Through

Try year(datepart(variable)))

R_E
Fluorite | Level 6 R_E
Fluorite | Level 6

Thanks, not exactly for my question

LinusH
Tourmaline | Level 20

See my answer in this thread:

https://communities.sas.com/thread/41166

An alternative would be to create a user defined format.

Data never sleeps
R_E
Fluorite | Level 6 R_E
Fluorite | Level 6

 

I wrote a proc format code , but it come out with error message 

here is the log attached.

but I still dont know how to correct it.Can you help me please.

I got one answer for initial question

year(intnx('year.4',datepart(time),1))

       

Thanks  

mfisher
Fluorite | Level 6

March 31 is always 275 days until the end of the calendar year (even during leap year), so this works:

fiscal_year=year(datepart(intnx('dtday', datetime_var,275)));

Reeza
Super User

year=(year(datepart(date))+1)-(month(datepart(date))<=3);

*example for dates, not date time;

data have;

    do i='01Jan2012'd to '31Dec2013'd;

        year=(year(i)+1)-(month(i)<=3);

        output;

    end;

    format i date9.;

run;

R_E
Fluorite | Level 6 R_E
Fluorite | Level 6

Thanks Reeza!
Awsome answer, it worked well.

R_E
Fluorite | Level 6 R_E
Fluorite | Level 6


Thanks Mifisher, your answer works well. sorry I was not allowed to choose two correct answer.

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

Register now

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 8 replies
  • 18257 views
  • 5 likes
  • 4 in conversation