BookmarkSubscribeRSS Feed
aditikarande
Calcite | Level 5

Hi,

 

I have a data set with the following format in date column: 28feb2018:00:00:00.000. How do I write a code to count all the observations with the year 2018, 2019 etc.?

 

Any help would be much appreciated.

 

Thanks a lot.

10 REPLIES 10
andreas_lds
Jade | Level 19

Is the variable numeric and a format attached displaying it as datetime, or is it an alphanumeric variable?

If you already have a sas-dateime variable, try:

proc summary data=have nway;
  class datetime_var;
  format datetime_var dtyear4.;
  output out=counted(drop=_type_ rename=(_freq_=count));
run;

Code is untested.

PeterClemmensen
Tourmaline | Level 20

Do something like this

 

data have;
   do dt = '01jan2018 00:00:00'dt to '31dec2020 00:00:00'dt by 3600;
      output;
   end;
   format dt datetime20.;
run;

proc freq data=have;
   table dt;
   format dt dtyear.;
run;
aditikarande
Calcite | Level 5
I am getting an error with the do statement
PeterClemmensen
Tourmaline | Level 20

Post your log please.

Jagadishkatam
Amethyst | Level 16

Please try the below code , please check the count dataset from proc freq

 

data have;
input datetime:anydtdtm.;
format datetime datetime20.;
cards;
28feb2018:00:00:00.000
;

data want;
set have;
year=year(datepart(datetime));
run;

proc freq data=want;
table year / out=count;
run;
Thanks,
Jag
aditikarande
Calcite | Level 5
What does have signify?
Jagadishkatam
Amethyst | Level 16
have is the input dataset, since you did not provide the sample dataset, i created a sample dataset names as have and then used in subsequently
Thanks,
Jag
aditikarande
Calcite | Level 5
also, what does cards signify?
Jagadishkatam
Amethyst | Level 16
In order to put the data using the input statement we can use either cards or datalines , indicating that after these statements the data portion starts
Thanks,
Jag