Help using Base SAS procedures

Date Variable

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 117
Accepted Solution

Date Variable

Hi all,

I am wondering if there is any way to create a new variable in a sas dataset that just populates the month and year from another variable in datetime9 format.

Example of what I mean:

Variable 1:

2009JAN14:23:00:00

Variable 2 (the one I want to generate) should look like:

JAN 2009

Example code will be much appreciated!


Accepted Solutions
Solution
‎11-30-2012 11:12 AM
Respected Advisor
Posts: 3,124

Re: Date Variable

If 'have' is your old data set with old datatime variable 'old_var', and 'want' is your new dataset with new variable 'new_var':

data want;

  set have;

    new_var=put(datepart(old_var),monyy7.);

run;

Haikuo

View solution in original post


All Replies
SAS Employee
Posts: 2

Re: Date Variable

Maybe this will work?


27   data slask;
28   x=datetime();
29   format x datetime9.;
30   var2=datepart(x);
31   put var2 date9.;
32   run;

30NOV2012

Respected Advisor
Posts: 3,124

Re: Date Variable

I believe what OP wanted is more of this one: (borrowed your code)

data slask;

  x=datetime();

  format x datetime9.;

  var2=put(datepart(x),monyy7.);

  put var2;

  run;

Haikuo

Frequent Contributor
Posts: 117

Re: Date Variable

that works but it just overwrites the dataset with 1 date. I want to keep all the records and generate a new column that has just the month and year.

How would this code be modified to do that?

Solution
‎11-30-2012 11:12 AM
Respected Advisor
Posts: 3,124

Re: Date Variable

If 'have' is your old data set with old datatime variable 'old_var', and 'want' is your new dataset with new variable 'new_var':

data want;

  set have;

    new_var=put(datepart(old_var),monyy7.);

run;

Haikuo

Super User
Posts: 10,466

Re: Date Variable

You may find that for many analysis purposes using an appropriate format may be better than adding multiple date containing variables. You could try the format MONYY7. for example. or create a custom format:

 

proc format library=work;

picture MyMonYear low-high = '%b %Y' (datatype=Date)

other='Invalid date';

run;

data _null_;

x= '01JAN2012'd;

put x= MyMonYear.;

run;

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 198 views
  • 0 likes
  • 4 in conversation