Question

I'm working on a data set (DATA) which has 3 variables (Var1, Var2, Var3) in a format I need to change. The variables are in a special date format (for example purposes, say OLDFMT1) and I need to change them into regular SAS date format using the DATEPART function.

The issue is that I need to accomplish this in a single data step using both a do loop and an array that calls the DATEPART function. The DIM function must be used used right in the array and I must drop the index varaible (i) before I end the datasetp.

Then, I have to apply the DATE9. function to these changed variables.

I'm fairly new to do loops and this one is causing me massive headaches. Any help would be greatly appreciated.

Was it helpful?

Solution

By saying you want to use the DATEPART function, you are implying that your three existing variables are currently stored as SAS "date-time" values. So try this:

data have;
  var1 = datetime();
  var2 = datetime();
  var3 = datetime();
  format var1-var3 datetime19.;
run;

data want;
   set have;
   array allvars(*) var1-var3;
   do i=1 to dim(allvars);
      allvars(i) = datepart(allvars(i));
      end;
   format var1-var2 date9.;
   drop i;
run;

Please remember that SAS has only two data types (numeric and character). Dates, times, and datetimes are only "known" as such when used with the appropriate format. In SAS:

  • A date is the number of days since January 1, 1960
  • A time is the number of seconds since midnight
  • A datetime is the number of seconds since January 1, 1960.

And notice that date9. is not a function; it is a format. There are a great many different formats that can be used to "exposed" the underlying value of the variable but the value itself does not change. That's one of the truly useful features of SAS programming.

OTHER TIPS

If you only want the data formatted as DATE9., rather than having to be stored as a date, then you can apply the prefix DT to any date format and apply it to a datetime value. So the DTDATE9. format will display a datetime value in the date9 format.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top