Turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

- Home
- /
- Analytics
- /
- Stat Procs
- /
- Date

Options

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page

🔒 This topic is **solved** and **locked**.
Need further help from the community? Please
sign in and ask a **new** question.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Posted 09-06-2021 03:11 PM
(498 views)

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

> 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

4 REPLIES 4

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

```
dt = datepart(date);
format dt yymmn6.;
```

since your "date" variable contains in fact a datetime value.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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

Paige Miller

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

> 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

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. **Registration is now open through August 30th**. Visit the SAS Hackathon homepage.

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.