08-31-2017 02:17 AM
Hi I would be gratefull if anyone could tell me if is there posibility for counting distinct values in summary or means procedure?
I know that I can do that by proc sql, however for cohesion of my code I would prefere use proc summary or means.
08-31-2017 04:00 AM
SUMMARY or MEANS alone don't do that, but a combination with PROC FREQ can do it:
proc freq data=sashelp.class noprint; table sex / out=class; run; proc summary data=class n; output out=want (drop=_type_); run;
08-31-2017 04:03 AM
Yes and no. SUMMARY/MEANS isn't built for that, so you have to jump through a hoop or two. For example, suppose you want to know how many DISTINCT STATE values appear in your data set:
proc summary data=have nway;
output out=stats (keep=state _freq_) n=;
Now the number of observations in STATS is the number of STATE values that appeared in the original data set HAVE. So it takes a little extra processing to capture that number, such as:
set stats nobs=n_states;
08-31-2017 04:13 AM
@Astounding's answer reveals a solution with two PROC SUMMARY steps:
proc summary data=sashelp.class nway; class sex; var age; * just some numeric variable for syntactical completeness; output out=class (keep=sex _freq_) n=; run; proc summary data=class n; output out=want (drop=_type_); run;
08-31-2017 04:32 AM
You can use proc means and proc summary, if you have a numeric variable in your data set or create a numeric variable for that purpose.
Data A; Do ID = 'A', 'B', 'T', 'Z'; Do NumVar = 1 To Ceil(Ranuni(1)*10); Output; End; End; Run; Proc Means Data=A NoPrint; By ID; Var NumVar; Output Out=Want (Keep=ID _FREQ_) N=; * remove _FREQ_ if not required; Run;
08-31-2017 08:39 AM - edited 08-31-2017 08:40 AM
PROC FREQ can do that.
ods select none; ods output nlevels=want; proc freq data=sashelp.class nlevels; table _all_; run; ods select all; proc print;run;
08-31-2017 01:08 PM - edited 08-31-2017 01:10 PM
I `m sorry I was not very clear, I need something like this
proc summary data=have missing;
class id1 id2 data;
var var1 var2 var3;
output out=wan sum(var1)= max(var2) n(DISTINCT var3)=;/
of course this way is not possible
I am able to do that, with two separate procedures and then merging result datasets, however, I was curious if it`s possible with one procedure or dataset just to make my code clearer.
08-31-2017 03:45 PM - edited 08-31-2017 03:51 PM
You can do what I want in proc sql:
create table want as
select id1, id2, date sum(var1), max(var2), count(distinct (var3)) from have;
I would like to know if there is option in proc summary/means for do the same.