athresh
athresh

Reputation: 553

creating datasets with input values in different formats

Can a dataset be created with the following date values which are in different formats?

29 JUN 66
July0100
19940421

Upvotes: 0

Views: 372

Answers (2)

BellevueBob
BellevueBob

Reputation: 9618

From the examples in your question, yes, you can create a variable that contains all those values, but it would have to be a character variable; it would not be a date. If you are asking if different observations (rows) of the same variable can have different SAS formats, then the direct answer is "no".

The key thing to understand is that SAS date variables are just numeric values that correspond to the number of days since January 1, 1960. You can display SAS dates in a variety of different ways using formats. However, a format does not affect the data value itself. To illustrate, run this program which prints the current system date using three different formats:

data _null_;
  today_is = today();

  put 'Using mmddyy10.: ' today_is mmddyy10.
    / 'Using yymmdd10.: ' today_is yymmdd10.
    / 'Using worddate.: ' today_is worddate.;
run;

To further illustrate, here is an example of creating a character variable using the three date styles in your question:

data _null_;
   length your_column $13;

/* 29 JUN 66  Assuming June 29, 1966 */
   test_date = '29jun1966'd;
   your_column = put(day(test_date),z2.) || ' ' 
              || upcase(put(test_date,monname3.)) || ' ' 
              || put(test_date,year2.);   
   put test_date= @18 your_column=;

/* July0100   Assuming July 1, 2000 */
   test_date = '1jul2000'd; 
   your_column = put(test_date,monname.) || put(test_date,year.);
   put test_date= @18 your_column=;

/* 19940421   Assuming April 21, 1994 */
   test_date = '21apr1994'd;
   your_column = put(test_date,yymmddn8.);
   put test_date= @18 your_column=;
 run;

Note there is not a natural SAS format for the first two values in your question.

Upvotes: 1

DJM
DJM

Reputation: 169

Not really. Or rather it depends on what you mean by date values in different formats.

You could, indeed, put them into a character variable on a data set. There's no problem with that, but then they'd essentially just be pieces of text.

But I'm guessing that's not really what you meant. The reason you can't store them all into one variable with different formats is that I don't believe you can apply more than one format simultaneously to any one variable.

You can, however, store it in multiple variables, each with a different format. Or alternatively store it in one variable, and then apply three different formats to it when you actually need to use it.

Upvotes: 2

Related Questions