BookmarkSubscribeRSS Feed
ayosas
Fluorite | Level 6
I am trying to use an array to create a date variable that combines individual day and month numeric values into a date in the year 1990 and recode to missing days and months that are -1, -13, -32


Libname D “directory”;
Data have;
Set D.have;
Format dob mmddyy8.;
Array datec {2} day_c month_c;
Do i = 1 to 2;
Datec{i} = mdy(day_c, month_c, 1990);
If day_c in (-1,-13) then day_c =.;
If month_c in (-1,-32) then month_c =.;

End;
8 REPLIES 8
Tom
Super User Tom
Super User

This does not really make any sense.

Please show some example input and what output you want for that input.

Reeza
Super User
This is technically possible, but a really strange use of arrays.
Since you will only reference these variables once you don't need an array at all.

What makes you think you need a loop?
ayosas
Fluorite | Level 6
Thanks, I am working on an assignment that requires me to use loops and arrays to create 3 date variables. So I was thinking that an array would be needed for each set of days and months
Tom
Super User Tom
Super User

In that case show an example of that situation.

Sounds like you say three sets of month,day, year variables.  Something like:

ID M1 D1 Y1 M2 D2 Y2 M3 D3 Y3 
1 1 1 1980 2 2 1990 3 3 2000

And you want to generate three date variables.

So you could create a separate array for each of the types of variables and then loop over an index into the array to apply the same logic to each of the arrays.

data want;
  set have;
  array m m1-m3;
  array d d1-d3;
  array y y1-y3 ;
  array date date1-date3;
  do index=1 to dim(m);
     date[index] = mdy(m[index],d[index],y[index]);
  end;
  format date1-date3 yymmdd10.;
  drop index;
run;

Result:

Obs  ID  M1  D1   Y1   M2  D2   Y2   M3  D3   Y3        date1       date2       date3

 1    1   1   1  1980   2   2  1990   3   3  2000  1980-01-01  1990-02-02  2000-03-03

If the values of the month variable are things like -1 and -13 in your example then the resulting DATE variable will be missing since such values cannot be used to generate a valid date.

 

If your real situation is different than my example then show a similar level of detail for your real problem.

pransh
Calcite | Level 5

hey,

wht if the year is not a variable in the data set and its just mentioned in the question that all of the month and date variable have year of 1990, then how to appraoch

pransh
Calcite | Level 5
your help will be really appreciated
Tom
Super User Tom
Super User

If you have a NEW question then start a new topic.

 

You can easily hard code the year, just use the actual value instead of a variable name.

date = mdy(month,day,1980);
pransh
Calcite | Level 5

hi i am solving a similar question did u get an proper answer

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 8 replies
  • 1720 views
  • 3 likes
  • 4 in conversation