BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
France
Quartz | Level 8

dear all,

 

I would like to create a quarter variable based on a date variable.

 

The FORMAT of the date is YYMMDDD10. (e.g.,1990-09-29). I use the code like 

DATA Step1.Appln_newQuarter;
   SET Step1.appln_new;
   IF MONTH(earliest_filing_date)=1 or 2 or 3 THEN quarter=1;
   ELSE IF MONTH(earliest_filing_date)=4 or 5 or 6 THEN quarter=2;
   ELSE IF MONTH(earliest_filing_date)=7 or 8 or 9 THEN quarter=3;
   ELSE IF MONTH(earliest_filing_date)=10 or 11 or 12 THEN quarter=4;   
RUN;

but the result is '1'. could you please give me some suggestions?

 

thanks in advance. 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20
DATA Step1.Appln_newQuarter;
   SET Step1.appln_new;
    quarter=qtr(earliest_filing_date);
     
RUN;

Use the qtr function  -- is all you need i think 

View solution in original post

2 REPLIES 2
novinosrin
Tourmaline | Level 20
DATA Step1.Appln_newQuarter;
   SET Step1.appln_new;
    quarter=qtr(earliest_filing_date);
     
RUN;

Use the qtr function  -- is all you need i think 

ballardw
Super User

@France wrote:

dear all,

 

I would like to create a quarter variable based on a date variable.

 

The FORMAT of the date is YYMMDDD10. (e.g.,1990-09-29). I use the code like 

DATA Step1.Appln_newQuarter;
   SET Step1.appln_new;
   IF MONTH(earliest_filing_date)=1 or 2 or 3 THEN quarter=1;
   ELSE IF MONTH(earliest_filing_date)=4 or 5 or 6 THEN quarter=2;
   ELSE IF MONTH(earliest_filing_date)=7 or 8 or 9 THEN quarter=3;
   ELSE IF MONTH(earliest_filing_date)=10 or 11 or 12 THEN quarter=4;   
RUN;

but the result is '1'. could you please give me some suggestions?

 

thanks in advance. 

 

 


For a very large number of purposes you may not even need to create an additional variable at all. SAS provides several formats for date values that will display quarters: QTRw. by default displays 1 to 4, QTRRw. which displays quarter as Roman numerals: I II III and IV, YYQw which will show year and quarter information together depending on width YYQ. default shows YYYYQn such as 2018Q1 or YYQxw. where you choose a charcter for x that controls the separator between the year and quarter numbe allowing such things as 2012/3 or 2017-2.

 

Analysis procedures will honor the formatted value for grouping purposes.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 4483 views
  • 0 likes
  • 3 in conversation