SAS Time Variable Question

I have a (perhaps) rather simple question. I am working on real time data that looks like this:

Sample Location

Date

Time

Concentration X

Concentration Y

Office

08/09/2009

1:00:00

2

7

Office

08/09/2009

1:00:10

5

5

Office

08/09/2009

1:00:30

5

6

There are several sampling locations, dates, and times. I would like to obtain average concentrations in these different areas, different dates (day of the week), and different times (morning, noon, evening, overnight).

I understand how to do this for sample location using proc means, but I get stumped when I need to do this for time and date variables.

I originally used proc means for different times of day but SAS did not understand this:

My questions are as follows:

1. Do I need to transform my 'Time' variable, because SAS does not seem to recognize this as time? If so, how do I do this. I was thinking of making a dummy variable for morning, noon, evening, overnight but SAS does not seem to recognize the time as it is in my database.

2. The same question but for Date. Are there any transformations that need to be made?

Re: SAS Time Variable Question

The CARDS statement (you can also use DATALINES statement if you are too young to remember what a card punch is) tells SAS to treat the following lines as data rather than commands. I just copied your data from the original post. I copied it out of the HTML table and just pasted it as raw text. So the last two numbers on each line of data are your X and Y concentration values. SAS stops reading when it sees a semi-colon ( on the line. You can also read from a text file by using an INFILE statement before the INPUT statement instead of the CARDS.

The INPUT statement tells it how to read the data. The first field is character, hence the $. The date and time fields have informats to tell SAS how to interpret the character strings as date and time. The : modifier tells it not to read too many characters. For example when the hour part of the time is less than 10 so that the time value only uses 7 characters instead of 8. The default informat is numeric so for the two concentration values I do not need to specify an informat.

The FORMAT statement tells SAS how to display the date and time variables so that humans can read them. I prefer to use Y/M/D order to display dates to avoid confusion between US default of M/D/Y and European default of D/M/Y. Also then the text string will sort in chronological order.

If your data is already in a dataset then you do not need to INPUT it. If you have it in a spreadsheet then you could try using PROC IMPORT to convert it to a dataset.

The INT(time/'06:00't) will divide the time (number of seconds since midnight) by the number of seconds in 6 hours and truncate to just the integer part. So you will get 0,1,2 or 3. I then added one so that the categories start from 1 instead of 0. To get them to display as you want you could use a format.

proc format ;

value tod 1='overnight' 2='morning' 3='afternoon' 4='evening' ;

run;

To create time of day categories as a character string using your time ranges you might just use IF/THEN logic. For example to create the categories as a character variable your could try:

Re: SAS Time Variable Question

The CARDS statement (you can also use DATALINES statement if you are too young to remember what a card punch is) tells SAS to treat the following lines as data rather than commands. I just copied your data from the original post. I copied it out of the HTML table and just pasted it as raw text. So the last two numbers on each line of data are your X and Y concentration values. SAS stops reading when it sees a semi-colon ( on the line. You can also read from a text file by using an INFILE statement before the INPUT statement instead of the CARDS.

The INPUT statement tells it how to read the data. The first field is character, hence the $. The date and time fields have informats to tell SAS how to interpret the character strings as date and time. The : modifier tells it not to read too many characters. For example when the hour part of the time is less than 10 so that the time value only uses 7 characters instead of 8. The default informat is numeric so for the two concentration values I do not need to specify an informat.

The FORMAT statement tells SAS how to display the date and time variables so that humans can read them. I prefer to use Y/M/D order to display dates to avoid confusion between US default of M/D/Y and European default of D/M/Y. Also then the text string will sort in chronological order.

If your data is already in a dataset then you do not need to INPUT it. If you have it in a spreadsheet then you could try using PROC IMPORT to convert it to a dataset.

The INT(time/'06:00't) will divide the time (number of seconds since midnight) by the number of seconds in 6 hours and truncate to just the integer part. So you will get 0,1,2 or 3. I then added one so that the categories start from 1 instead of 0. To get them to display as you want you could use a format.

proc format ;

value tod 1='overnight' 2='morning' 3='afternoon' 4='evening' ;

run;

To create time of day categories as a character string using your time ranges you might just use IF/THEN logic. For example to create the categories as a character variable your could try: