New SAS User

Completely new to SAS or trying something new with SAS? Post here for help getting started.
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

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 10 replies
  • 2446 views
  • 2 likes
  • 4 in conversation