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
?
Date=catx("/", of Month Year);
Date=catx("/", of Month Year);
Thanks a lot @naveenraj. Do you also now how I change the format ddmmyy10. (eg. 01/02/2011) to this mm/year (02/2011)?
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.
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.;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.