0

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.

2
  • Allow me to clarify, as I was not specific enough. The 3 variables have 3 completely different names (ex: var1, secondOne, thirdvar) and are not in consecutive places (that is, they are not in columns 1 - 3; but in something like column 8, 24, 41) Also, as a helpful contributor pointed out, Date9. is a format, not a function. Commented Apr 5, 2013 at 2:00
  • You can still use Bob's solution as is; he uses a shortcut to define the array, but you can still use it - array allvars var1 secondOne thirdvar; will work exactly as his list does. Commented Apr 5, 2013 at 3:20

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.