Create quarterly identifier

Accepted Solution Solved
Reply
Contributor
Posts: 63
Accepted Solution

Create quarterly identifier

I have a dataset "2010_4", with all records pertaining to quater 4 of year 2010. I want to make a new quarterly indentifier variable that identifies each records as quater 4 of 2010. I have tried making this as a character variable ("2010Q4",  "2010-4", or "2010_4") as below. Somehow SAS forces itself to see "qtr" variable as numeric. How can this be fixed?

Also, is there a way to make this variable into date format, therefore numeric? I know for monthly data, you could count each month as day 1 of that month. Can you do something similar to quarter? Thanks.

 

data 2010_4;
set 2010_4;

format qtr $6.;

qtr = "2010Q4";
year = 2010;
run;


Accepted Solutions
Solution
‎08-09-2016 12:34 PM
Contributor
Posts: 21

Re: Create quarterly identifier

I would suggest making the QTR variable numeric so that SAS can handle it using its date routines. You can create the value using the YYQ function and display it using the YYQ format as shown here:

 

data _null_;

  year = 2010;

  qtr = yyq(2010, 4);

  format qtr yyq6.;

  put (year qtr) (=);

run;

 

year=2010 qtr=2010Q4

 

View solution in original post


All Replies
Solution
‎08-09-2016 12:34 PM
Contributor
Posts: 21

Re: Create quarterly identifier

I would suggest making the QTR variable numeric so that SAS can handle it using its date routines. You can create the value using the YYQ function and display it using the YYQ format as shown here:

 

data _null_;

  year = 2010;

  qtr = yyq(2010, 4);

  format qtr yyq6.;

  put (year qtr) (=);

run;

 

year=2010 qtr=2010Q4

 

Contributor
Posts: 63

Re: Create quarterly identifier

I seem to be able to achieve what I want by taking out this line:
put (year qtr) (=);

what does this line do exactly? It just seem to endlessly print "year=2010 qtr=2010Q4" on my screen.
Grand Advisor
Posts: 10,251

Re: Create quarterly identifier

You don't say if you already have any date information in your data set. Adding the variable if you are assigning the value manually is one line of code, see the input below. The format used to show the value is key later.

data example;
   yq = input("2010Q4",YYQ7.);
   format yq yyq.;
   put "Quarter as basic date= " yq mmddyy10. " quarter as year=" yq year4. " quarter as quarter " yq yyq6.;
run;
Contributor
Posts: 63

Re: Create quarterly identifier

Thanks to the solutions given above. What does the put command line do exactly? Looks like it just prints out the specified text in the command. After running your codes, the quarter variables shows a 5 digit number as the number of days from 1/1/1960. How can I see it as '2010Q4'?

Grand Advisor
Posts: 10,251

Re: Create quarterly identifier

PUT sends output to log. It is often used with small datasets to demonstrate the appearance of something. You would not normally include that in processing a large dataset for each line unless sending the output to a text file.

 

The five digit number is to be displayed with a format.

That is the reason I used PUT to show the results with different formats, those bits of code with period after them.

Include

 FORMAT YQ<or whatever you name your quarter variable> YYQ6. ;

to set the default display of the quarter variable into the 2014Q4 appearance.

 

Contributor
Posts: 63

Re: Create quarterly identifier

Thank you for that explanation. And do you happen to know what I need to do, if I simply want to make a character variable, with value equal to "2010Q4"?
Contributor
Posts: 63

Re: Create quarterly identifier

so now i have a qtr variable euqal to "2010Q4" and is in YYQ6. format. If I now do a proc means, where qtr is equal to some value. How would I do that? I tried this but it doesn't work.

 

where qtr = '2012Q1'd;

Grand Advisor
Posts: 10,251

Re: Create quarterly identifier

[ Edited ]

Please show error messages when something "doesn't work".

 

Following your previous post you created qtr as CHARACTER. '2012Q1'd is attempting to be a DATE literal and corresponds to numeric value. But date literals are always day of month, month year , 'ddMONyyyy'd.

So I suspect that you got a message about mismatched data types.If QTR is character you compare it to "2012Q4" no D.

 

At this point you are into a different topic and possibly should be in a different thread.

 

Note, dealing with character equivalent of date values is a frequent topic for involving "how do I determing difference between two values, whether a value is within x days/weeks/months/quarters/year" and such. Almost every solution involves first creating a date value. You are likely to be much better off in the long run with date values instead of character dates. For instance, if your QTR was date valued your code should work.

Contributor
Posts: 63

Re: Create quarterly identifier

sorry for making things confusing. i am asking this in the context of having qtr as a numeric date variable. the error messages:

ERROR: Invalid date/time/datetime constant '2012Q1'd.
ERROR: Syntax error while parsing WHERE clause.
Grand Advisor
Posts: 10,251

Re: Create quarterly identifier

Exactly as the error says. Date literals are in the form of "ddMONyy"d or "ddMONYYYY"d. No others are accepted. But the is the variable you are comparing to a date or a character variable.

Contributor
Posts: 63

Re: Create quarterly identifier

i made that change and now it works fine. so i guess when the variable is in date format, regardless if it's a day, month or quarter, always refer to it with either "ddMONyy"d or "ddMONYYYY"d. thank you.
☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 11 replies
  • 592 views
  • 0 likes
  • 3 in conversation