sas >> Extracting Quarter from date

by randistan69 » Mon, 13 Sep 2004 12:09:34 GMT

I have dates in the format of yymmdd8. I want to have a separate
variable of quarters...My code is

data x; set y;
format quarter yyq4. ;
quarter = qtr(date);
run;

All that this code does is 60Q1 for all the dates in my file...the
dates are from Jan 1 1999 to 31 dec 2003...so there should be 20
quarters...PLEASE help

sas >> Extracting Quarter from date

by MOORTHYE » Mon, 13 Sep 2004 13:04:26 GMT


Hi Pal,



You just neer to remove the quarter function.



With QTR function, You are trying to apply the format YYQ4. to the resultant
value (i.e 1,2,3,4), which SAS treats as negative
years(-0001,-0002,-0003,-0004).

Your flow :



Quarter=qtr(Jan 1 1999) > results 1 (1st quarter), and then you are applying
the format yyq4. to 1.



So, just say



data x; set y;

format quarter yyq4. ;

quarter = date;

run;



Cheerz
Easwara







-----Original Message-----
From: Microstructure [mailto: XXXX@XXXXX.COM ]
Sent: Monday, September 13, 2004 12:10 PM
To: XXXX@XXXXX.COM
Subject: Extracting Quarter from date



I have dates in the format of yymmdd8. I want to have a separate

variable of quarters...My code is



data x; set y;

format quarter yyq4. ;

quarter = qtr(date);

run;



All that this code does is 60Q1 for all the dates in my file...the

dates are from Jan 1 1999 to 31 dec 2003...so there should be 20

quarters...PLEASE help

sas >> Extracting Quarter from date

by Roger Lustig » Mon, 13 Sep 2004 18:49:47 GMT

Another way to go:
quarter=intnx('quarter',date,0);
format quarter yyq4.;
run;

This creates a variable with value representing the first day in DATE's
quarter. Could be handy if working with otherwise-ordered data.

Roger

sas >> Extracting Quarter from date

by Nathaniel_Wooding » Mon, 13 Sep 2004 19:45:30 GMT

Microstructure

Please run the following code:

data a;
do date= '01jan1999'd to '31dec2001'd by 1;
quarter=qtr(date);
date2=date;
qrtextra=quarter;
output;
end;
proc print ;
format date mmddyy10. qrtextra date2 yyq4.;
run;


I have added duplicate values of date and quarter so that they may be
displayed using different formats.

One point to make first is that if you have a true sas date, ie, the number
of days since Jan 1, 1960, it is not stored in a particular format but
rather a format may be associated with it. Hence, the value stored is some
number of days and this value may be displayed in many formats.

In your code, you first extract the value of quarter which is simply 1
through 4 but you then print it with the format yyq4. This format is
intended to display SAS dates as quarters and shows the year first. Hence,
if you apply this format to the extracted values of quarter, 1, 2, 3, or 4,
they will display as 60Q1 since they are all in the first quarter of 1960
and, recall, that SAS dates start on 1/1/60. If you apply a SAS date format
to 1,2,3, and 4, SAS simply sees them as Jan 1, 1960, etc.

Nat Wooding



Microstructure
<randistan69@HOTM To: XXXX@XXXXX.COM
AIL.COM> cc:
Sent by: "SAS(r) Subject: Extracting Quarter from date
Discussion"
< XXXX@XXXXX.COM
GA.EDU>


09/13/04 12:09 AM
Please respond to
Microstructure






I have dates in the format of yymmdd8. I want to have a separate
variable of quarters...My code is

data x; set y;
format quarter yyq4. ;
quarter = qtr(date);
run;

All that this code does is 60Q1 for all the dates in my file...the
dates are from Jan 1 1999 to 31 dec 2003...so there should be 20
quarters...PLEASE help

sas >> Extracting Quarter from date

by Rob » Mon, 13 Sep 2004 23:08:25 GMT

The problem is with your understanding of the QTR function and the FORMAT
statement. If you remove the FORMAT statement, your code would yield a
number in the range 1-4, representing the quarter of the SAS date - but
without the year. The QTR function does not advertise that it returns the
year:

data x;
input date mmddyy6.;
*format quarter yyq4. ;
quarter = qtr(date);
put date=mmddyy10. quarter=;
cards;
010198
100103
010104
040104
070104
run;

DATE=01/01/1998 QUARTER=1
DATE=10/01/2003 QUARTER=4
DATE=01/01/2004 QUARTER=1
DATE=04/01/2004 QUARTER=2
DATE=07/01/2004 QUARTER=3
NOTE: The data set WORK.X has 5 observations and 2 variables.
NOTE: DATA statement used:
real time 0.06 seconds
cpu time 0.00 seconds

Since the numbers 1-4 all fall into the first quarter represented by a SAS
date variable, your format produces the same result - accurately. What you
really want can be accomplished with the PUT function - using, in fact, that
same format:

data x;
input date mmddyy6.;
quarter = put(date, yyq4.);
put date=mmddyy10. quarter=;
cards;
010198
100103
010104
040104
070104
run;

DATE=01/01/1998 QUARTER=98Q1
DATE=10/01/2003 QUARTER=03Q4
DATE=01/01/2004 QUARTER=04Q1
DATE=04/01/2004 QUARTER=04Q2
DATE=07/01/2004 QUARTER=04Q3
NOTE: The data set WORK.X has 5 observations and 2 variables.
NOTE: DATA statement used:
real time 0.01 seconds
cpu time 0.00 seconds

HTH,

Rob Rohrbough

Similar Threads

1. convert to date - month and quarter

Please help!  I need to convert the following:

I have two columns as numeric values:
Quarter         month
20001         200001
20002         200004
20003         200008
20004         200012

I would like to convert these to SAS dates and just return the Month Value and Quarter Value.

Quarter    Month
1             JAN
2             APR
3             AUG
4             DEC

Thanking you in advance!!
Sincerely, Joan





---------------------------------
Do you Yahoo!?
 The all-new My Yahoo! Get yours free!

2. converting text to date to quarter

3. Merge if a date is within two quarters?

Hi,

   Suppose I want to merge two files like this:

CAR.TXT

CAR         QUARTER      BRAKELEVEL
TOYOTA    1992q1           100
TOYOTA    1992q2           300
TOYOTA    1992q3
TOYOTA    1992q4
TOYOTA    1993q1           120
HONDA  1995q1
HONDA 1995q2
FORD    1992q1           300
FORD    1992q2           200
FORD    1992q3           900
FORD    1992q4
FORD    1993q1           220

ACCIDENT.txt

ACCIDENTDATE  CAR
1992/01/02           TOYOTA
1992/02/02            FORD
2000/01/01            HONDA

I want to merge the above two files into one file
MERGED.txt:
CAR         ACCIDENTDATE  BRAKELEVEL
TOYOTA     1992/01/02             100
FORD         1992/02/02             300
HONDA       2000/01/01           missing data


How do I do this? Note that CAR.txt does not contain information
regarding Honda on 2000/01/01.

4. SAS code to extract system date

5. extracting dates

Hi all,

I was wondering if anyone has any suggestions as how to code the
following problem:

I have multiple observations per studyid, but need to keep only one
per studyid. I would like to extract the last date for each studyid
(if moveout=0 within the same studyid), EXCEPT when moveout=1 - in
this case I would like to keep only that row for that particular
studyid.

Here is a sample of my data. The arrows show the rows I need to keep:

studyid   moveout          date
1            0                     1999
1            0                     2001
1            0                     1998
1            0                     2002 <--
2            0                     2003
2            1                     2004 <--
2            0                     2005
2            1                     2006
3            0                     2007
3            0                     2000
3            0                     2001<--
3            1                     2002<--
3            0                     2003
4            0                     2006
4            0                     2001<--

Thanks very much in advance.

Maja

6. Extracting date in required form from macro variable and concatinating with string

7. Extracting days between two dates

Dear friends,

I have a data set like that:

Subject     Date_1      Date_2
   1        25/12/2004  28/12/2004
   2        26/12/2004  27/12/2004

and I would like to obtain one register for each day between 
Date_1 and Date_2 in a new data set like:

Subject     Weekday_date 
   1        25/12/2004
   1        26/12/2004
   1        27/12/2004
   1        28/12/2004
   2        26/12/2004
   2        27/12/2004

Thank you in advance for your suggestions,

Guillermo

8. Extracting date and time info from irregular strings to great