The sql procedure below gave me "The query requires remerging summary statistics back with the original data." I'd like to group the results so I have one row for each st and one column for each yr.
data a;
input yr 4. st $2. typ 1.;
datalines;
2014AL1
2014AL2
2014AL3
2014AL1
2015AL2
2015AL3
2014AR1
2015AR1
2015AR2
;
run;
proc sql;
select st,
case when yr=2014 then mean(case when typ=1 then 1 else 0 end) else . end as pct14,
case when yr=2015 then mean(case when typ=1 then 1 else 0 end) else . end as pct15
from a
group by st;
quit;
Sometimes, SQL isn't the right tool.
data a;
input yr 4. st $2. typ 1.;
if typ=1 then value=1; else value=0;
datalines;
2014AL1
2014AL2
2014AL3
2014AL1
2015AL2
2015AL3
2014AR1
2015AR1
2015AR2
;
run;
proc report data=a;
columns st yr,value;
define st/group "State";
define yr/across "Year";
define value/mean "Percent" format=percent7.0;
run;
something like
ST 2014 2015
AL 50% 0%
AR 0% 50%
Sometimes, SQL isn't the right tool.
data a;
input yr 4. st $2. typ 1.;
if typ=1 then value=1; else value=0;
datalines;
2014AL1
2014AL2
2014AL3
2014AL1
2015AL2
2015AL3
2014AR1
2015AR1
2015AR2
;
run;
proc report data=a;
columns st yr,value;
define st/group "State";
define yr/across "Year";
define value/mean "Percent" format=percent7.0;
run;
ok, I was trying to avoid the additional data step but I accept that sql isn't well equipped in this case.
But it's not an "additional step", where you had PROC SQL before, you now have PROC REPORT, that's the same number of steps.
@PaigeMiller wrote:
But it's not an "additional step", where you had PROC SQL before, you now have PROC REPORT, that's the same number of steps.
AND fewer lines of code.
The if statement would have required an additional step given my original data step, but I accept your larger point.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.