BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
vomer
Obsidian | Level 7

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!

1 ACCEPTED SOLUTION

Accepted Solutions
Haikuo
Onyx | Level 15

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

5 REPLIES 5
Lena_sas
SAS Employee

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

Haikuo
Onyx | Level 15

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

vomer
Obsidian | Level 7

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?

Haikuo
Onyx | Level 15

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

ballardw
Super User

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;

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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