Pyrite | Level 9

## Combining two numeric variables to make a Monyyyy Date?

I have been messing around with this for nearly 20 minutes now and looking up several other threads. I am sure you experienced SAS users are about done with date questions, but can some explain what is wrong with my code?

``````Data BMI_DOB;
Set BMI_year;
DOB = INPUT(catx(DOB_M, DOB_Y), date7.);
format DOB date7.;
run;``````

Why is it converting them to character values rather than dates?

``````22         GOPTIONS ACCESSIBLE;
23         Data BMI_DOB;
24         	Set BMI_year;
25         	DOB = INPUT(catx(DOB_M, DOB_Y), date7.);
26         	format DOB date7.;
27         run;

NOTE: Numeric values have been converted to character values at the places given by: (Line):(Column).
25:19
NOTE: Invalid argument to function INPUT at line 25 column 8.
ID=1 Gender=2 DOB_M=9 DOB_Y=1981 Race=4 Year=1997 Height=67 Weight=145 BMI=22.707730007 DOB=. _ERROR_=1 _N_=1
NOTE: Invalid argument to function INPUT at line 25 column 8.
ID=1 Gender=2 DOB_M=9 DOB_Y=1981 Race=4 Year=1998 Height=67 Weight=150 BMI=23.490755179 DOB=. _ERROR_=1 _N_=2
NOTE: Invalid argument to function INPUT at line 25 column 8.
ID=1 Gender=2 DOB_M=9 DOB_Y=1981 Race=4 Year=1999 Height=67 Weight=150 BMI=23.490755179 DOB=. _ERROR_=1 _N_=3
NOTE: Invalid argument to function INPUT at line 25 column 8.
ID=1 Gender=2 DOB_M=9 DOB_Y=1981 Race=4 Year=2000 Height=68 Weight=160 BMI=24.325259516 DOB=. _ERROR_=1 _N_=4
NOTE: Invalid argument to function INPUT at line 25 column 8.
ID=1 Gender=2 DOB_M=9 DOB_Y=1981 Race=4 Year=2001 Height=67 Weight=163 BMI=25.526620628 DOB=. _ERROR_=1 _N_=5
NOTE: Invalid argument to function INPUT at line 25 column 8.``````

1 ACCEPTED SOLUTION

Accepted Solutions
Meteorite | Level 14

## Re: Combining two numeric variables to make a Monyyyy Date?

-  Your using CATX() function with in appropriate arguments. CATX(delimiter, item1,item2....) first argument is treated as delimiter. You      just need CATS() instead.

- SAS will automaticallly convert numeric values to character with best. format if arguments to CAT functions are numeric.

- Notes are generated because values and format for INPUT() function doesn't match.

If you want to suppress this kind of notes then don't print them in log by setting options errors=0;

Since you have month and year only the best way is to use MDY() function, if you don't have day value simply put some dummy and use the format of month and year in which you need.

``````data want;
set have;
want=mdy(dob_m,01,dob_y);
format want monyy7.;
run;``````

Thanks,
Suryakiran
10 REPLIES 10
Pyrite | Level 9

## Re: Combining two numeric variables to make a Monyyyy Date?

I should add so you don't have to search the code that DOB_M is a numeric value 1-12 representing Month and DOB_Y is a 4 character numeric value representing year.

Tourmaline | Level 20

## Re: Combining two numeric variables to make a Monyyyy Date?

Are you after this?

``````data have;
do month=1 to 12;
year=2018;
output;
end;
run;

data want;
set have;
want=input(cats(year,month),yymmn6.);
format want date9.;
run;
``````
Opal | Level 21

## Re: Combining two numeric variables to make a Monyyyy Date?

The first argument to the catx function is a separator string to be inserted between concatenated parts. DOB_M being the separator, that leaves only DOB_Y as a string to be concatenated; DOB_M will not be used.

PG
Pyrite | Level 9

## Re: Combining two numeric variables to make a Monyyyy Date?

Thank you for this! When I do something incorrectly, I like to know why- you gave me just that!
Meteorite | Level 14

## Re: Combining two numeric variables to make a Monyyyy Date?

-  Your using CATX() function with in appropriate arguments. CATX(delimiter, item1,item2....) first argument is treated as delimiter. You      just need CATS() instead.

- SAS will automaticallly convert numeric values to character with best. format if arguments to CAT functions are numeric.

- Notes are generated because values and format for INPUT() function doesn't match.

If you want to suppress this kind of notes then don't print them in log by setting options errors=0;

Since you have month and year only the best way is to use MDY() function, if you don't have day value simply put some dummy and use the format of month and year in which you need.

``````data want;
set have;
want=mdy(dob_m,01,dob_y);
format want monyy7.;
run;``````

Thanks,
Suryakiran
Quartz | Level 8

## Re: Combining two numeric variables to make a Monyyyy Date?

Hello @SuryaKiran  You wrote-- "The log will show the notes because your are trying to concatenate numeric values, so SAS will automaticallly convert numeric values to character with best. format"

No, cat family of functions does not write any such auto conversion notes in the sas log.

And only the best way is to use MDY() function?

Wait until what the OP wants in his/her final result before assuming what is the best way although mdy is very straight forward, that I completely agree.

Nevertheless, we are not sure of OP's expected output-

should it revert to first of month, end or whatever, does that OP want a resulting character? OP needs to provide more info

Super User

## Re: Combining two numeric variables to make a Monyyyy Date?

Regardless of what OP wants day to be, MDY is likely more efficient due to the use of less functions.
Meteorite | Level 14

## Re: Combining two numeric variables to make a Monyyyy Date?

Agree, thanks for pointing that out @Allaluiah. Although SAS will automatically converts with best. format no notes is generated. Probably the notes is generated by invalid arguments to  INPUT() function.

Thanks,
Suryakiran
Tourmaline | Level 20

## Re: Combining two numeric variables to make a Monyyyy Date?

@SuryaKiran and @Allaluiah  It's very trivial, the question, the discussion about autoconversion.etc imo. Best bet is to test and confirm . 🙂

There are some functions/concepts like within hash/app that do not auto convert and no notes but just an incorrect output and error. However that is something beyond the scope of this thread anyway

Pyrite | Level 9

## Re: Combining two numeric variables to make a Monyyyy Date?

I was thinking about doing this with a dummy variable like you said. I don't know why I didn't try.

Thank you! This is exactly what I was looking for.

May I ask if you see this, is my code correct beside the catx? I could do the same thing with cats? I am going to look up the cat family.

Thanks again.
Discussion stats
• 10 replies
• 6387 views
• 7 likes
• 6 in conversation