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

I have date variables in the form of YYYY-MM-DD, stored in tables.

 

How do I extract the Year?

 

Basically I just want to do a simple SQL 

such as, select * from table where YEAR(date) = 2017;

 

Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
gamotte
Rhodochrosite | Level 12

Hello,

the function you are looking for is year() as you guessed correctly, but your data has to be in

a date format.

 

data have;
format dt yymmdd10.;
input dt yymmdd10.;
datalines;
2016-05-21
2017-08-12
2014-03-02
;
run;

data want;
set have;
where year(dt)=2017;
run;

Edit: If you have strings instead :

data want;
set have;
where year(input(dt,yymmdd10.))=2017;
run;

View solution in original post

7 REPLIES 7
gamotte
Rhodochrosite | Level 12

Hello,

the function you are looking for is year() as you guessed correctly, but your data has to be in

a date format.

 

data have;
format dt yymmdd10.;
input dt yymmdd10.;
datalines;
2016-05-21
2017-08-12
2014-03-02
;
run;

data want;
set have;
where year(dt)=2017;
run;

Edit: If you have strings instead :

data want;
set have;
where year(input(dt,yymmdd10.))=2017;
run;
katkarparam
Fluorite | Level 6

Do you have another solution to change observation value at
one shot for all rather than one by one. suppose i have billions of record...

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Are you sure its a SAS numeric date, i.e. it is a number if you remove the format.  If so then year(), if it is character which I suspect, then just use:

where scan(date,1,"-")="2017";

 

afiqcjohari
Quartz | Level 8

The date is in this format YYMMDD10. 

afiqcjohari
Quartz | Level 8

Can I create a new column year within proc sql?

 

proc sql;
select input(thedate,yymmdd10.) as myyear
from mytable;
quit;

 

I tried this but doesn't seem to work.

Singla14
Calcite | Level 5
Hi , I have input file having year ( let's say 2015 to 2017 YYYY) .
I am trying to fetch system year ( year(today())
And then compare year from input file to fetched year.
If matches , those rows should come in output.

If comparison for years is not working ..Have tried reading input year in chat..Integer both..But nothing working.
Can some one please help..Urgent !
RW9
Diamond | Level 26 RW9
Diamond | Level 26

This question is answered and closed.  Please start a new thread providing example data in the form of a datastep and what the output should look like.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 7 replies
  • 43660 views
  • 1 like
  • 5 in conversation