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-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!

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.

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