Evan
Evan

Reputation: 7

Using array to create date in SAS with only day and month

I need to use the day and month variables to create a date variable using the array function. For example, let's say the year is 2022, which needs to be included in the date.

I tried the following codes, but it doesn't seem to work. SAS gave me a number but not sure that's the correct date. Thanks for your help.

array day{1} day_1;
array month{1} month_1;
array date{1} date_1;
do i=1 to dim(day);
date{i}=MDY(month{i},day{i},2022);
end;
format date_1 mmyydd10.;
run;

Upvotes: 0

Views: 557

Answers (1)

Tom
Tom

Reputation: 51566

Your posted code does not have a DATA statement telling SAS what dataset you want to create. Nor anyway to find the existing DAY_1 or MONTH_1 variables. If the variables are in an existing SAS dataset then add a SET statement.

If there is only one DAY variable and one MONTH variable there is no need for the ARRAY or the DO loop.

data want;
  set have;
  date_1=MDY(month_1,day_1,2022);
  format date_1 mmyydd10.;
run;

If you do have multiple variables then include them in the arrays and the FORMAT statement.

data want;
  set have;
  array day   day_1-day_3
  array month month_1-month_3;
  array date  date_1-date_3;
  do i=1 to dim(day);
    date{i}=MDY(month{i},day{i},2022);
  end;
  format date_1-date_3 mmyydd10.;
  drop i;
run;

Upvotes: 2

Related Questions