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

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;

1 ACCEPTED SOLUTION

Accepted Solutions
RickAster
Obsidian | Level 7

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

11 REPLIES 11
RickAster
Obsidian | Level 7

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

 

apolitical
Obsidian | Level 7
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.
ballardw
Super User

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;
apolitical
Obsidian | Level 7

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'?

ballardw
Super User

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.

 

apolitical
Obsidian | Level 7
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"?
apolitical
Obsidian | Level 7

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;

ballardw
Super User

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.

apolitical
Obsidian | Level 7
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.
ballardw
Super User

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.

apolitical
Obsidian | Level 7
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.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 11 replies
  • 4324 views
  • 0 likes
  • 3 in conversation