Question

I have following problem:

I need to write a begin and end date into a matrix. Where the matrix contains the yearly quarters (1-4) in the collumns and the rows are the year.

E.g.

Matrix:

Q1 Q2 Q3 Q4 
            2010
            2011

Now the Date 01.01.2010 should be put in the first element and the date 09.20.2011 in the sixed element.

Thanks in advance.

Was it helpful?

Solution

You first have to consider that SAS does not actually have date/time/datetime variables. It just uses numeric variables formatted as date/time/datetime. The actual value being:

  • days since 1/1/1960 for dates
  • seconds since 00:00 for times
  • seconds since 1/1/1960 00:00 for datetimes

SAS does not even distinguish between integer and float numeric types. So a date value can contain a fractional part.

What you do or can do with a SAS numeric variable is completely up to you, and mostly depends on the format you apply. You could mistakenly format a variable containing a date value with a datetime format... or even with a currency format... SAS won't notice or complain.

You also have to consider that SAS does not even actually have matrixes and arrays. It does provide a way to simulate their use to read and write to dataset variables.

That said, SAS does provide a whole lot of formats and informats that allow you to implement date and time manipulation.

Assuming you are coding within a data step, and assuming the "dates" are in dataset numeric variables, then the PUT function can extract the datepart you need to calculate row, column of the matrix element to write to, like so:

DATA table;
   ARRAY dm{2,4} dm_r1c1-dm_r1c4 dm_r2c1-dm_r2c4;
   beg_row = PUT(beg_date, YEAR4.)-2009;
   end_row = PUT(end_date, YEAR4.)-2009;
   beg_col = PUT(beg_date, QTR1.);
   end_col = PUT(end_date, QTR1.);
   dm{beg_row,beg_col} = beg_date;
   dm{end_row,end_col} = end_date;
RUN;

... or if you are using a one-dimensional array:

DATA table;
   ARRAY da{8} da_1-da_8;
   beg_index = 4 * (PUT(beg_date, YEAR4.)-2010) + PUT(beg_date, QTR1.);
   end_index = 4 * (PUT(end_date, YEAR4.)-2010) + PUT(end_date, QTR1.);
   da{beg_index} = beg_date;
   da{end_index} = end_date;
RUN;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top