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

Is there a way to combine these 3 PROC SQL code blocks into 1 PROC SQL code block? The only 2 parts of the SQL code that change are the name of the count(x) variable and the date range used in the where statement.

PROC SQL;
CREATE TABLE comp_date_range AS
SELECT company,
count(x) as ee_quarter20_count
from company_data
where doi between '01Aug2020'd and today()
group by company
;
run;quit;

 

PROC SQL;
CREATE TABLE comp_date_range1 AS
SELECT company,
count(x) as ee_quarter19_count
from company_data
where doi between '01Aug2019'd and intnx('year',today(),-1,'same')
group by company
;
run;quit;

 

PROC SQL;
CREATE TABLE comp_date_range2 AS
SELECT company,
count(x) as ee_ytd_count
from company_data
where doi between '01Jan2020'd and today()
group by company
;
run;quit;

 

Thank you.

 

1 ACCEPTED SOLUTION

Accepted Solutions
ChrisNZ
Tourmaline | Level 20

Like this?

select company
     , sum(doi between '01Aug2020'd and today()) as ee_quarter20_count
     , sum(doi between '01Aug2019'd and today()) as ee_quarter19_count
from company_data;
group by company;

 

View solution in original post

4 REPLIES 4
ChrisNZ
Tourmaline | Level 20

Like this?

select company
     , sum(doi between '01Aug2020'd and today()) as ee_quarter20_count
     , sum(doi between '01Aug2019'd and today()) as ee_quarter19_count
from company_data;
group by company;

 

gzr2mz39
Quartz | Level 8

Yes, thank you @ChrisNZ that's what I'm looking for.

But if I want to sum "x" then shouldn't it be like this?

 

PROC SQL;
CREATE TABLE contractors_date_range_a AS
SELECT company,
sum(case when doi between '01Aug2020'd and today() then x else 0 end) as ee_quarter_count
from company_data
group by company
;
run;quit;

 

Tom
Super User Tom
Super User

@gzr2mz39 wrote:

Yes, thank you @ChrisNZ that's what I'm looking for.

But if I want to sum "x" then shouldn't it be like this?

 

PROC SQL;
CREATE TABLE contractors_date_range_a AS
SELECT company,
sum(case when doi between '01Aug2020'd and today() then x else 0 end) as ee_quarter_count
from company_data
group by company
;
run;quit;

 


Yes. If you wanted change the program to generate the sum of X instead of just counting the number of non-misisng values of X. 

 

Note if you did still want the count then you might use:

count(case when (doi between '01Aug2020'd and today()) then x else null end)
PGStats
Opal | Level 21

You could define the summarizing periods in a separate dataset:

 

data periods;
startDate =  '01Aug2020'd; endDate = today(); period = "Qtr 20"; output;
startDate =  '01Aug2019'd; endDate = intnx('year',today(),-1,'same'); period = "Qtr 19"; output;
startDate =  '01Jan2020'd; endDate = today(); period = "Ytd"; output;
run;

proc sql;
create table comp_date as
select
	period,
	company,
	count(x) as count_x
from
	periods, company_data
where doi between startDate and endDate
group by period, company;
quit;
PG

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

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
  • 4 replies
  • 1481 views
  • 2 likes
  • 4 in conversation