variable
variable

Reputation: 1023

Format all date variables the same

How can I sellect all variables that contain dates and apply a specific format to them? I don't know ahead of time their names or their number.

Something like this pseudocode, although the special variable reference DATES doesn't exist as NUMERIC or CHARACTER or ALL.

DATA test;
    date1 = '31DEC2015';
    name = "names";
    first_n = "charge";
    anotherdate = 1000;
    result = 34.2;
    again_dates = 1001;
Run;

DATA test2;
    set test1;
    FORMAT _DATES_ date9;
Run;

If this is not possible, then let's say that the date variables are all already formatted in ddmmyy10 for example and I'd like to select all variables with such formatting and convert them to date9.

Upvotes: 1

Views: 1835

Answers (2)

Reeza
Reeza

Reputation: 21264

You can do your second question, but not your first. SAS stores dates as numbers so there's no way to differentiate any integer from a date.

You can filter the dates with the ddmmyy format from either sashelp.vcolumn or using proc contents and then I would use proc datasets to apply these changes. The advantages in using proc datasets is that you're not reprocessing the data set.

The solution I'm suggesting is very close to Christopher Anderson, but different data source and implementation, but the code is essentially the same.

Upvotes: 0

Christopher Anderson
Christopher Anderson

Reputation: 176

I'm not sure if the more general question you're asking has a solution (at least I don't know of what could be done to grab any possible date variable that might be hiding as a numeric variable, unless there's some naming conventions that are being followed perhaps). But if you just want to reformat an unspecified number of date variables that have one of several known formats to date9., that's more straightforward:

DATA test;
format anotherdate  again_dates  date1 ddmmyy10.;
    date1 = '31DEC2015';
    name = "names";
    first_n = "charge";
    anotherdate = 1000;
    result = 34.2;
    again_dates = 1001;
Run;


proc contents data = test out = testcntnts;
run;

proc sql;
select NAME into: datevars separated by ' ' from testcntnts where FORMAT = "DDMMYY";
quit;


data test2;
format &datevars date9.;
set test;
run;

Upvotes: 2

Related Questions