DATA Step, Macro, Functions and more

How do I create a new date-variable?

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 15
Accepted Solution

How do I create a new date-variable?

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

 

?


Accepted Solutions
Solution
‎04-18-2016 06:12 AM
Contributor
Posts: 53

Re: How do I create a new date-variable?

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

View solution in original post


All Replies
Solution
‎04-18-2016 06:12 AM
Contributor
Posts: 53

Re: How do I create a new date-variable?

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

Occasional Contributor
Posts: 15

Re: How do I create a new date-variable?

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

Super User
Super User
Posts: 7,401

Re: How do I create a new date-variable?

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.

Super User
Posts: 17,831

Re: How do I create a new date-variable?

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.;
☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

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