Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to use YEAR()

Status
Not open for further replies.

cosmid

Programmer
Feb 14, 2008
73
US
Hi,

I know how to use the YEAR() with raw data using with input statement. like below:

DATA dates;
INPUT name $ 1-4 @6 bday mmddyy11.;
m=MONTH(bday);
d=DAY(bday) ;
y=YEAR(bday);
wk_d=WEEKDAY(bday);
q=QTR(bday);
CARDS;
John 1 1 1960
Mary 07/11/1955
Joan 07-11-1955
Kate 11.12.1962
Mark 06081959
;
RUN;

PROC PRINT DATA=dates;
VAR bday m d y;
FORMAT bday date9. ;
RUN;

My question is, how to use YEAR() with an existing data? I tried so many websites on this and i still can not find any solution. I have a .dat file with dates in it, I just need to generate a frequency table based on year. The dates are in mmddyy format. I need to extract the year from the mmddyy format like YEAR(), but I can not make it to work. So I copied all the data using proc print and then used the steps from above. I guess my question is how to create and use variables for permanent data files. They only have examples on temporary data like the one shown above. Thanks!
 
You need to save the data into a SAS dataset. In the example given this is the temp dataset named 'dates' (WORK.dates). What you need to do in your case is 'read in' your .dat file into a SAS dataset. (You need to know the structure as well. I presume that your '.dat' file is in the same structure as the example posted.)

Code:
filename _infil "c:\anyfilde.dat";

DATA dates;
  infile _infil dsd;
  INPUT name $ bday;
  m = MONTH(bday);
  d = DAY(bday) ;
  y = YEAR(bday);
  wk_d = WEEKDAY(bday);
  q = QTR(bday);
RUN;
proc freq data = dates;
tables year*q;
run;

I just used the proc freq to show you how its done. You can rewrite the freq call so that you get the data you need.

Klaz

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top