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!
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
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
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
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
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;
Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.
Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.
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.