1
votes

I have two numeric variables, year and month. year variable has data such as 2010 and month variable has data such as 1 and 10 (1 through 9 doesn't have zero at the front). I need to combine these two variables and then convert it to YYMMn6. format so that I can merge another dataset based on the date.

For example, the input is:

2012 1 2012 10

The output I want is (in YYMMn6. format): 201201 201210

The codes I tried so far:

year1=close_year;
year2=clse_month;
yearmonth = cats(of year1-year2); *this results in character variable;
DATE2 = INPUT(PUT(yearmonth,8.),YYMMN6.);
FORMAT DATE2 YYMMN6.;

Of course I get an error message. Thanks.

2
What error are you getting? Your code works perfectly for me.user2337871
Do you want a character string or a date? A date is just a numeric. A format is nothing but direction on how to present that number for human viewing. Show us what your data look like and we can better help you understand what is going on and how to best accomplish your goal.DomPazz
Among other things, why are you doing put(yearmonth,8.) and then inputting with YYMMN6.? The main problem you will have is that the month needs to be two digits, which it isn't for you.Joe

2 Answers

3
votes

With numeric variables I'd use MDY function rather than putting and whatnot; you're having trouble here because 20101 isn't a valid YYMM value.

dateval = mdy(monthval,1,yearval);
format dateval yymmn6.;

Note that the 'final' date format is wholly unrelated to whatever you use to input the date variable from an informat; there's no difference from SAS's point of view between

dateval = input('01JAN2010',DATE9.);
format dateval YYMMN6.;

and

dateval = input('201001',YYMMN6.);
format dateval YYMMN6.;

The input/informat is converting a value into a numeric number of days since 1/1/1960. The final format is telling SAS how to display that newly created number.

1
votes

You can use the answer mentioned by Joe which

  • would give you the flexibility to change to a different format if you want later on, without any hassle.
  • would keep the variables in numeric format, so mathematical or date functions would be easy to apply.

or you can use

mydate=put(mdy(monthval,1,yearval),yymmn6.);

if you want the output in char format.

Both are correct. Choose as per your requiremnt.