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

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;

 

1 ACCEPTED SOLUTION

Accepted Solutions
sbxkoenk
SAS Super FREQ

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

View solution in original post

4 REPLIES 4
sbxkoenk
SAS Super FREQ

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

PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
sbxkoenk
SAS Super FREQ

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

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

Register now

What is ANOVA?

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.

Discussion stats
  • 4 replies
  • 1324 views
  • 2 likes
  • 4 in conversation