DATA Step, Macro, Functions and more

All month in a year

Reply
Contributor Boa
Contributor
Posts: 21

All month in a year

[ Edited ]

Hi sasusers, i have a dataset of 300000 + datas and with orderdate as my only column and it contains Datetime as well.

How can i show all the month in a 2008? For example, Jan , Feb , Mar without the days value. I am doing this for a bar chart wizard.

 

Some data in my orderdate:

 

Orderdate

13OCT07  00:00:00

2JUN08    00:00:00

2JAN08    00:00:00

1JAN08    00:00:00

1DEC08    00:00:00

5DEC09    00:00:00

 

 

PROC Star
Posts: 768

Re: All month in a year

are your date/datetime variables stored as you have posted? Smiley Happy

Contributor Boa
Contributor
Posts: 21

Re: All month in a year

Yes they are stored in the dataset already.   is it possible to just show the month in the year 2008 instead of just printing the date and month together?

Super User
Super User
Posts: 7,997

Re: All month in a year

Hi,

 

Well:

data want;
  set have (where=(year(datepart(orderdate))=2008));
  mnth=month(datepart(orderdate));
run;

This now only contains 2008 data, and has a variable mnth which contains the 2 digit month (which you can format to other text if you want, look up MONNAME. format).

Contributor Boa
Contributor
Posts: 21

Re: All month in a year

[ Edited ]

Firstly, thanks for the code. I tried and used it for my program, the code looks fine but there is no output in the orderdate. 

 

i would like to ask why would you put a bracket behing a where clause? if you put that, the where clause wont run and it shows error. 

And what is mnth? am i creating a new variable just for month itself? Kindly advise. Thanks Smiley Happy 

Super User
Super User
Posts: 7,997

Re: All month in a year

[ Edited ]

Hi,

 

Well, you should post some test data - in the form of a datastep - so I can run it and see.  I cant tell offhand what is happening without code/log/test data.  Follow this post (only need a few rows):

https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat...

 

The where clause is correct, it is an option on the input dataset, so the where is applied as the data is read.  You can put dataset options in several places:

https://onlinecourses.science.psu.edu/stat481/node/16

 

If your getting an error, then its likely to be a typo or something.

 

Example:

data have;
  orderdate="13OCT2007:00:00:00"dt; output;
  orderdate="02JUN2008:00:00:00"dt; output;
  orderdate="02JAN2008:00:00:00"dt; output;
  format orderdate datetime.;
run;

data want;
  set have (where=(year(datepart(orderdate))=2008));
  mnth=month(datepart(orderdate));
run;

 

Mnth is a new variable, it is numeric, and contains the month number within the year, i.e. Jan=1.  You can then format this number using monname to display as Jan.

Contributor Boa
Contributor
Posts: 21

Re: All month in a year

ok thanks., i have another question that is similar to this, i probably would post another question. But i doubt it would use monname3. 

Super User
Posts: 11,343

Re: All month in a year

Displaying values is often best accomplished with a Format. SAS provides tools to create custom formats for date, time and datetime values that you can customize.

 

proc format ;
  picture dtmon (default=3)
  . = " "
  low - high = '%b'   (datatype=datetime)
  ;
run;

Use the dtmon format in any procedure you want to display the 3-letter month abbreviation. This example will print the first 10 observations using the format:

 

proc print data=work.date (obs=10);
   var orderdate;
   format orderdate dtmon.;
run;
Valued Guide
Posts: 2,177

Re: All month in a year

it is about the bar-chart wizard (not the data).
In SAS/Graph, there is an approach which solves this problem by defining all the "mid-points" you want
Ask a Question
Discussion stats
  • 8 replies
  • 310 views
  • 0 likes
  • 5 in conversation