Help using Base SAS procedures

how to use proc sql convert datetime to year?

Accepted Solution Solved
Reply
Occasional Contributor R_E
Occasional Contributor
Posts: 8
Accepted Solution

how to use proc sql convert datetime to year?

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

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

                                                            '



Accepted Solutions
Solution
‎03-20-2013 12:19 PM
Super User
Posts: 17,949

Re: how to use proc sql convert datetime to year?

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


All Replies
Super User
Posts: 17,949

Re: how to use proc sql convert datetime to year?

Assuming its Proc SQL and not SQL Pass-Through

Try year(datepart(variable)))

Occasional Contributor R_E
Occasional Contributor
Posts: 8

Re: how to use proc sql convert datetime to year?

Thanks, not exactly for my question

Super User
Posts: 5,260

Re: how to use proc sql convert datetime to year?

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
Occasional Contributor R_E
Occasional Contributor
Posts: 8

Re: how to use proc sql convert datetime to year?

 

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  

Attachment
Contributor
Posts: 58

Re: how to use proc sql convert datetime to year?

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)));

Solution
‎03-20-2013 12:19 PM
Super User
Posts: 17,949

Re: how to use proc sql convert datetime to year?

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;

Occasional Contributor R_E
Occasional Contributor
Posts: 8

Re: how to use proc sql convert datetime to year?

Thanks Reeza!
Awsome answer, it worked well.

Occasional Contributor R_E
Occasional Contributor
Posts: 8

Re: how to use proc sql convert datetime to year?


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

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 8 replies
  • 10341 views
  • 5 likes
  • 4 in conversation