I have the variable "date" whose format is in "03APR2020:00:00:00.000", but I want to make a new variable "dt" using only "202004". What code can I use?
Code below is wrong:
data tabl1;
set tabl;
dt = year(date) + month(date);
run;
> Advice: don't create your own text strings or numbers to represent dates. If you do that, and then at some point need to know what 6 months before 202004 is, you then have to program your own calculations as well.
You are absolutely right !!
I was too quick with my answer (and its variable 'c'), sorry. 🙄
Koen
Try something like this.
Use concatenation operator (|| or !!) and not the plus-sign.
Or use CATx functions.
data have;
a='03APR2020:00:00:00.000'dt;
put a= dtmonyy7.;
b=datepart(a);
put b= monyy7.;
put b= yymmdd5.;
c=year(b)||'-'||put(month(b),z2.);
put c=;
run;
Koen
dt = datepart(date);
format dt yymmn6.;
since your "date" variable contains in fact a datetime value.
Advice: don't create your own text strings or numbers to represent dates. If you do that, and then at some point need to know what 6 months before 202004 is, you then have to program your own calculations as well.
If you use SAS date values, these will always be more useful then your own text strings. Example from @Kurt_Bremser shows how. Then, calculations are built into SAS via the INTCK and INTNX function, so you don't have to program these calculations yourself.
> Advice: don't create your own text strings or numbers to represent dates. If you do that, and then at some point need to know what 6 months before 202004 is, you then have to program your own calculations as well.
You are absolutely right !!
I was too quick with my answer (and its variable 'c'), sorry. 🙄
Koen
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
ANOVA, or Analysis Of Variance, is used to compare the averages or means of two or more populations to better understand how they differ. Watch this tutorial for more.
Find more tutorials on the SAS Users YouTube channel.