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!
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;
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;
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...
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";
The date is in this format YYMMDD10.
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.
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.
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.
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.