Store variables in proc
I am running the following code:
PROC MEANS DATA=count;
class advertiser;
var date;
output out = countdata N=Count;
RUN;
The dataset output gives me the variables Advertiser, Count, Type, Frequency. I would like to know how I can store other variables in the dataset without doing any analysis on them. For example, just keep the variable date that lists all dates.
source to share
PROC MEANS
has an operator ID
that allows you to do some of what you ask.
Since it PROC MEANS
sums data, if you need multiple rows per class
value, you cannot get it directly; it is best to combine the results PROC MEANS
into a dataset using merge
.
However, if you only need one line for a class value, and you have variables that are always the same for the same class variable, or you are fine with the maximum / minimum value, you have parameters.
If there is only one value (the value of the class variables), you can simply add the variable to the statement class
or to the statement by
. The latter might be better in some cases if you have a lot of class values (as it will bind more resources to use it as a class versus).
If you have multiple values (or can) and you don't want them in the class
or statements by
, you can use the ID
. This, by default, puts the maximum value in the output dataset (or with an option IDMIN
in the statement PROC
, puts the minimum value).
proc means data=sashelp.class;
class age;
types age;
id name;
var height weight;
output out=classvars mean=;
run;
Identity variable name
looks silly, but shows an example.
source to share
PROC MEANS is summed over class variables. If you need more variables in the output dataset, you can list them in the class statement. PROC SQL will allow you to compute counts using a grouped variable and then output a dataset with the same number of records as the input dataset, with a counter column added ("summary summary statistics"). They are all close to what you want:
proc means data=sashelp.class nway;
class sex age;
var height;
output out = want N=Count_Height;
run;
proc sql;
create table want2 as
select *,count(height) as Count_Height
from sashelp.class
group by sex
;
quit;
source to share