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

Dear all SAS-users,

 

I have this:

 

Obs     Month     Year

1          2             2011

2          3             2011

3          5             2011

4          6             2012

5          6             2012

6          7             2012

7          7             2012

8          8             2013

 

How do I create a new variable that looks like this:

 

Date

2/2011

3/2011

5/2011

6/2012

6/2012

7/2012

7/2012

8/2013

 

?

1 ACCEPTED SOLUTION

Accepted Solutions
naveenraj
Quartz | Level 8

Date=catx("/", of Month Year);

View solution in original post

4 REPLIES 4
naveenraj
Quartz | Level 8

Date=catx("/", of Month Year);

Gothardt
Obsidian | Level 7

Thanks a lot @naveenraj. Do you also now how I change the format ddmmyy10. (eg. 01/02/2011) to this mm/year (02/2011)?

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Well, @naveenraj's solution creates a character variable.  Unless that is really the only use for the variable, then it is probably not an ideal solution.  So you have a year, and a month, so the next thing to do is decide on a rule for day - as date variables need to be complete.  Lets take an example of setting 01 as day for each:

new_date=mdy(month,1,year);

This will create a numeic variable which contains the number of days since a specific date (some 50 od years ogo), which is how SAS stores dates.  You can then apply one of the many formats to this number to a see a date value:

format new_date date9.;

Will display as 01JAN2015.

 

It is always a good idea to store dates/times as numerics and apply formats, as this allows the use of all functions, and maths on them, and allows you to simply change the format it displays in.

Reeza
Super User

You need a date variable, not a character, otherwise you can't do calculations based on date or change how it's displayed. 

 

The code below should help:

 

Date_var = mdy(month, day, year);

Format date_var ddmmyy10.;

sas-innovate-2024.png

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.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 4 replies
  • 37959 views
  • 5 likes
  • 4 in conversation