BookmarkSubscribeRSS Feed
Cheesiepoof05
Obsidian | Level 7

Hello,


I have a date formatted as "Numeric" like YYYYMMDD ie 20220927.  I would like to convert that to Year and Quarter such as 2022Q3.  I'm using a Proc sql statement to hit against another table (SummaryLookup) to bring back the date and need to add a line to create a second field to convert it to yyyyQq please.

 

Proc sql;
create table DATE_TEST as
Select

dt_id_YYYYMMDD,


From SummaryLookup;

quit;

13 REPLIES 13
Kurt_Bremser
Super User

You only need to change the format of the SAS date variable to YYQ with PROC DATASETS.

Statistical procedures (FREQ, SUMMARY, TABULATE) will use the formatted values to build groups.

Cheesiepoof05
Obsidian | Level 7

Thank you but I'm not sure how to put that into my Proc sql; code.

ballardw
Super User

@Cheesiepoof05 wrote:

Thank you but I'm not sure how to put that into my Proc sql; code.


You don't have to. Just apply the format when you need it such as in a report or analysis.

 

What result do you see with this code:

proc freq data= SummaryLookup;
   tables dt_id_YYYYMMDD;
   format dt_id_YYYYMMDD yyq6.;
run;

If your variable is a SAS date value then you should see the values shown in YYYYQN display.

If not, then you may have to create a new variable.

Patrick
Opal | Level 21

Please provide the report created with code as below so we know what we're dealing with (SAS Date value, just a number or a string). Once we know we can go from there.

Proc Contents data=SummaryLookup(keep=dt_id_YYYYMMDD);
run;quit;
Cheesiepoof05
Obsidian | Level 7

This is the code I'm using.  It's just hitting against a temporary table where I've already pulled the data from.  "adjudication_dt_id" is a "numeric" field with 8 digits.  I'm just wanting to create a second field where it takes that numeric field and makes it yyyyQq.  So 20220418 in the first field would be 2022Q2 in the second field.

 

Proc sql;
create table DATE_TEST as
Select
adjudication_dt_id

From SummaryLookup;

quit;

 

adjudication_dt_id
20190105
20210907
20200430
20221231
AMSAS
SAS Super FREQ

I'd suggest converting the numeric value into a SAS date value
SAS Date, Time, and Datetime Values 
Then you can use SAS date formats 

Better still would be to track back where adjudication_dt_id is created and create/read it as a SAS Date value to start with 

Here's an example:

data _null_  ;	
	file "test.txt" ;
	infile cards ;
	input ;
	put _infile_ ;
cards ;
20230123
20221205
20220401
;

data readText ;
	infile "test.txt" ;
	input myDate yymmdd8. ;
	put "no format : " myDate  ;
	put "date7.    : " myDate date7. ;
	put "mmyy.     : " myDate mmyy. ;
	put "yyq.      : " myDate yyq. ;
	put "===========================" ;
run ;

 

Cheesiepoof05
Obsidian | Level 7

I got it figured out.  I appreciate everyone's time.  I went with:

 

substr(cat(adjudication_dt_id),1,4) as Year,
substr(cat(adjudication_dt_id),5,2) as Month,
Case
WHEN CALCULATED Month = '01' THEN "Q1"
WHEN CALCULATED Month = '02' THEN "Q1"
WHEN CALCULATED Month = '03' THEN "Q1"
WHEN CALCULATED Month = '04' THEN "Q2"
WHEN CALCULATED Month = '05' THEN "Q2"
WHEN CALCULATED Month = '06' THEN "Q2"
WHEN CALCULATED Month = '07' THEN "Q3"
WHEN CALCULATED Month = '08' THEN "Q3"
WHEN CALCULATED Month = '09' THEN "Q3"
WHEN CALCULATED Month = '10' THEN "Q4"
WHEN CALCULATED Month = '11' THEN "Q4"
WHEN CALCULATED Month = '12' THEN "Q4"
ELSE "ERROR"
END AS Quarter,
catx('',CALCULATED Year, CALCULATED Quarter) AS YEAR_QTR,

ballardw
Super User

For slightly better, as in "not as long"

Case
WHEN CALCULATED Month in ( '01' '02' '03') THEN "Q1"
WHEN CALCULATED Month in ( '04' '05' '06') THEN "Q2"
WHEN CALCULATED Month in ( '07' '08' '09') THEN "Q3"
WHEN CALCULATED Month in ( '10' '11' '12') THEN "Q4"
ELSE "ERROR"
END AS Quarter,

But in the long run you would be better off turning that not a SAS date value into a SAS date value.

 

There are multiple functions that allow to do many things with actual dates such as determine intervals between values (how many years/months/quarters/weeks ), see if a date falls on a holiday or which day of the week and many others.

Cheesiepoof05
Obsidian | Level 7

Thank you.

jebjur
SAS Employee

Turning your character date string into a numeric SAS date value with the INPUT function is a much easier option (lot less coding), and then you can apply the YYQw. format to the SAS date value so it displays in the style you want:

 

data SummaryLookup;
input adjudication_dt_id $;
cards;
20190105
20210907
20200430
20221231
;
run;

proc sql;
create table date_test as select
adjudication_dt_id, input(adjudication_dt_id, yymmdd8.) as new_dt_id format=yyq6.
from SummaryLookup;
quit;

Kurt_Bremser
Super User

@Cheesiepoof05 wrote:

I got it figured out.  I appreciate everyone's time.  I went with:

 

substr(cat(adjudication_dt_id),1,4) as Year,
substr(cat(adjudication_dt_id),5,2) as Month,
Case
WHEN CALCULATED Month = '01' THEN "Q1"
WHEN CALCULATED Month = '02' THEN "Q1"
WHEN CALCULATED Month = '03' THEN "Q1"
WHEN CALCULATED Month = '04' THEN "Q2"
WHEN CALCULATED Month = '05' THEN "Q2"
WHEN CALCULATED Month = '06' THEN "Q2"
WHEN CALCULATED Month = '07' THEN "Q3"
WHEN CALCULATED Month = '08' THEN "Q3"
WHEN CALCULATED Month = '09' THEN "Q3"
WHEN CALCULATED Month = '10' THEN "Q4"
WHEN CALCULATED Month = '11' THEN "Q4"
WHEN CALCULATED Month = '12' THEN "Q4"
ELSE "ERROR"
END AS Quarter,
catx('',CALCULATED Year, CALCULATED Quarter) AS YEAR_QTR,


This is a very fine (in fact, textbook) example for stupid data causing stupid code.

With a proper date value, this mess of WHENs turns into one simple function call. Depending on the following analysis, this whole step might not be necessary at all, as one can always use the proper format to let statistical procedures create the wanted groups. 

SASKiwi
PROC Star

Here is a much better way of doing what you want:

Proc sql;
create table DATE_TEST as
Select
input(adjudication_dt_id, yymmdd8.) as adjudication_dt format = yyq6.
From SummaryLookup;
quit;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 13 replies
  • 2237 views
  • 2 likes
  • 7 in conversation