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.
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;
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;
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;
@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)
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;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.