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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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