@cawalsh wrote:
I am trying to run descriptive statistics for, let's say, a state instead of on the entire country. How do I run descriptive statistics for my filtered state?
Also, if I filter my data, how can I save my filtered data as a new table? Thank you!
Think carefully about why you are saving a filtered data set. If you are thinking of creating one data set for each state that is generally a poor idea except possibly for a report on each state.
SAS has a very important concept called BY group processing. That means that each level of a variable or combination of variables can be processed for the values as a group using BY syntax.
If you split data up then to do the same thing on each set you have to modify or duplicate code to point to each dataset, possibly creating yet another output data set. Eventually creating lots of harder than necessary code to maintain and update.
AND you would need to know the combinations of variables that have values as some things work poorly with empty data sets.
Example of BY group processing with two variables:
proc sort data=sashelp.class out=work.class;
by sex age;
run;
proc means data=work.class;
by sex age;
var height weight;
run;
Creates summary statistics of n, mean, min, max and standard deviation of height and weight for all the levels of Sex and Age in the data set. I didn't need to know the 11 combinations of the two variables, creating 11 separate data sets or code to summarize 11 different sets.
If I only want summaries of males 14 or older :
proc sort data=sashelp.class out=work.class;
by sex age;
run;
proc means data=work.class;
by sex age;
var height weight;
run;
BY group processing requires that a data set be sorted by the by variables referenced on the BY statement. Options in sorting such as DESCENDING if used in sorting also need to be used in the BY statement as otherwise the procedure will assume ascending sort.