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;
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
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
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;
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'?
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.
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;
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.
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.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.