How can I display a single pie chart in SAS using gchart - sas

So I have seven different fields/variables in a SAS table each containing 1's and 0's. I need to - if at all possible - display these seven variables in one single pie. Is this possible? If so how? When I do this: pie variable1 variable2 / options I get two pies. Is there a way for me to combine them into one?

If your indicators reflect percentages of a whole WITHOUT overlap then yes you can.
This applies to multiple choice questions where you can select One of the Above. If it's a question that is select All that apply, then this would not be appropriate.
You cannot use the GCHART procedure but first need to summarize your data. Use a proc means to calculate the sums and then pass those to your PROC.
Proc means data = have stackods sum;
VAR ind1-ind7;
ODS OUTPUT summary=totals;
Run;
Use the TOTALS dataset in your gchart with the sum as the Pie statement. I don't recall what the variable is called.

Related

SAS-sgplot-add label to bar clusters

I am looking for an option to add label to bar clusters.
I want the values: 0.940, 0.250, 0.520, 0.580, and 0.230 in the middle of the clustered bars, like the following:
Assuming you have SAS 9.4 TS1M2 or later, you have an option, seglabel, which specifically does this, other than the 'display only once', which I don't think is a thing you can get out of the box.
proc sgplot data=sashelp.cars;
vbar cylinders/group=origin groupdisplay=cluster seglabel;
run;
If you don't, or you need more control than that gives you (such as your request to display only once), there are options, such as in my paper, Labelling without the hassle; it is for stacked bar charts, but the general approach would work for clustered also, you'd just have to adjust things some.
This gets somewhat close, and probably would get nearly perfect for your case; it needs some customization for groups that don't have all three group values present, but you don't have that in your example.
More complex solutions exist using annotations or GTL, as well, with this general approach (of overlaying on the precomputed bars a label).
proc summary data=sashelp.cars;
class origin cylinders;
types origin*cylinders;
var mpg_city;
output out=cars_summary(drop=_:) n(mpg_city)=count;
run;
data cars_labels;
set cars_summary;
if origin='Asia' then count_asia=count;
if origin='USA' then count_usa=count;
if origin='Europe' then count_eu=count;
ypos = floor(count/2);
run;
proc sgplot data=cars_labels;
vbarparm category=cylinders response=count/group=origin groupdisplay=cluster;
scatter x=cylinders y=ypos/markerchar=count_asia discreteoffset=-0.35;
scatter x=cylinders y=ypos/markerchar=count_usa discreteoffset=0.20;
scatter x=cylinders y=ypos/markerchar=count_eu discreteoffset=-0.1;
run;

SAS - Totaling values in a column by a different column's value

The title may be a little ambiguous. Essentially, using the SASHELP.SHOES dataset, I'm trying to summarize the data in a new table by totaling the Sales, and Returns for each region. For instance, instead of having 56 rows for shoes sold in Africa and their individual sales/returns values, I have one row for Africa with columns TotalSales and TotalReturns. I need to do this for each region in the original dataset.
I'm not familiar at all with SAS, this is more or less the first thing I've really had to program in it. I've tried a few variations of data steps with IN or WHERE conditions, proc means steps with SUM() statements, and DO/DO WHILE loops, but I've missed something each time.
In Proc MEANS
Use a CLASS statement to specify which variable(s) are to be used to group the data. In your case REGION.
Use the VAR statement to specify which variable(s) are to have statistics calculated for within each grouping.
Default output
Corresponding to the minimal syntax
ods listing;
proc means noprint data=SASHELP.SHOES;
class region;
var sales returns;
output out=shoes_stats;
run;
Creates data set WORK.SHOES_STATS with one row per statistic per region.
Other output structure
Use procedure option NWAY to only get summarizations for combinations of all the CLASS variables. (In your case this corresponds to rows with _TYPE_=1)
The output columns can have the statistic name automatically concatenated to the variable name using the OUTPUT statement option / autoname.
Use data set options to control variables that are kept or dropped.
proc means nway noprint data=SASHELP.SHOES;
class region;
var sales returns;
output out=shoes_sums(drop=_type_ _freq_) sum= / autoname;
run;
dm 'vt shoes_sums; column names' viewtable;

Compare datasets and plot in sas

I have two variables in 2 separate datasets in sas. Both have a primary key of Customer_Id and another column say LVR . One dataset has old values for the LVR Column. The other one has values from the new calculation for the same column.
I need to show the differences between both on a graph.
I tried to merge them and then tried proc gplot to plot the two LVRs.
Merged dataset looks something like this :
Cust_id LVR_new LVR_old
111 1 2
222 2 .
333 5 4
The dataset containing LVR_new is almost twice in size (number of rows) than the one containing LVR_old.We got more customers qualifying post the new calculations.
The merged dataset has 3046778 observations and 3 variables.
I tried to use proc gplot using the code below:
proc plot data=djia;
plot LVR_old*LVR_new = Cust_id;
run;
This has been running since long so i don't expect the results are going to be very useful.
Can anyone please suggest how can I achieve this. I need to showcase the differences between the two datasets on a graph to be able to show the shift in the results.
Thanks!
Why not use PROC TTEST? There are some ODS GRAPHICS plots that PROC TTEST makes.
Your problem looks exactly liked the paired comparisons example in the documentation.
http://support.sas.com/documentation/cdl/en/statug/67523/HTML/default/viewer.htm#statug_ttest_examples03.htm
proc ttest;
paired LVR_old*LVR_new;
run;

How do I create a pie chart in SAS for a variable whose values I've grouped using PROC FORMAT?

I have a variable ideology that takes on values from 1 to 7. I've decided to make these continuous values into three groups using PROC FORMAT like so:
proc format;
value ideofmt
1-2='Lib or Extr Lib'
3-5='Mod Lib, Slight Lib or Slight Cons'
6-7='Cons or Extr Cons';
run;
I want to create a pie chart that takes into account my grouping of these continuous values, if possible without having to modify the data itself. What I've tried is:
proc gchart data=sasuser.project2;
pie ideology /noheading percent=arrow slice=inside value=inside coutline=black
woutline=2;
format ideology ideofmt.;
run;
This gets me a pie chart that has the group labels that I want, but the pie chart is split into 7 slices (corresponding to the 7 values) instead of 3 (corresponding to the 3 groups).
Any help with this would be much appreciated. Thanks!
Add the discrete option to your pie statement:
proc gchart data=sasuser.project2;
pie ideology /noheading percent=arrow slice=inside value=inside coutline=black discrete
woutline=2;
format ideology ideofmt.;
run;
quit;
Otherwise, I think SAS assigns slices based on quantiles of your data, ignoring formatted values.
Also, a quit statement is required at the end of proc gchart in addition to the run statement.

SAS proc Freq & gchart display additional value's frequency/ bars

This might be a weird question. I have a data set contains data like agree, neutral, disagree...for many questions. There is not so many observations so for some question, one or more options has frequency of 0, say neutral. When I run proc freq, since neutral shows up in that variable, the table does not contain a row for neutral. I end up with tables with different number of rows. I would like to know if there is a option to show these 0 frequency rows. I will also need to run proc gchart for the same data set, and I will run into the same problem for having different number of bars. Please help me on this. Thank you!
This depends on how exactly you are running your PROC FREQ. It has the sparse option, which tells it to create a value for every logical cell on the table when creating an output dataset; normally, while you would have a cell with a missing value (or zero) in a crosstab, if that is output to a dataset (which is vertical, ie each combination of x and y axis value are placed in one row) those rows are left off. Sparse makes sure that doesn't happen; and in a larger (n-dimensional) crosstab, it creates rows for every possible combination of every variable, even ones that don't occur in the data.
However, if you're just doing
proc freq data=mydata;
tables myvar;
run;
That won't help you, as SAS doesn't really have anything to go on to figure out what should be there.
For that, you have to use a class variable procedure. Proc Tabulate is one of such procedures, and is similar to Proc Freq in its syntax (sort of). You need to either use CLASSDATA on the proc statement, or PRINTMISS on the table statement. In the former case, you do not need to use a format, I don't believe. In the latter case (PRINTMISS), you need to create a format for your variable (if you don't already have one) that contains all levels of the data that you want to display (even if it's just an identity format, e.g. formatting character strings to identical character strings), and specify PRELOADFMT on the proc statement. See this man page for more details.