Somebody posted a macro but I do not know how to use it
This topic has 3 sections:
- The original question
- The posted answer
- The notes explaining how to use the macro
The original question
I am creating a large number of bar charts. For each chart I must exclude any categories that have fewer than 10 students. I have been running frequencies and then manually clicking on each category that was to be omitted, but I know there must be a better way. Any ideas would be greatly appreciated.
The posted answer
DATA LIST LIST /age prog year. BEGIN DATA 25 1 2 25 1 2 25 1 2 25 1 2 25 1 4 25 1 4 25 1 4 25 1 4 25 1 4 25 1 4 25 1 7 25 1 7 25 1 7 25 1 7 25 1 2 26 1 2 27 2 1 27 2 1 27 2 1 27 2 1 27 2 1 27 2 1 END DATA. COMPUTE dummy=1. SET MPRINT=no. *////////////////////. DEFINE !chart (minnb=!TOKENS(1) /cat=!TOKENS(1)) FILTER OFF. * Sorting is necessary for the MATCH FILES command. SORT CASES BY !cat. !LET !fname=!QUOTE(!CONCAT('C:\temp\',!cat,'.SAV')) AGGREGATE /OUTFILE=!fname /PRESORTED /BREAK=!cat /nb = N(dummy). MATCH FILES /FILE=* /TABLE=!fname /BY !cat. FREQUENCY VARIABLES=!cat. COMPUTE flag=(nb>=!minnb). FILTER BY flag. GRAPH /BAR(SIMPLE)=COUNT BY !cat /MISSING=REPORT. * Delete the variable nb. MATCH FILES FILE=* /DROP=nb. !ENDDEFINE. *////////////////////. SET MPRINT=yes. !chart minnb=5 cat=age. !chart minnb=6 cat=prog. !chart minnb=5 cat=year. * The above method is ok if you need to do other things between the printing of the charts. * The following macro is ok if all you need is to print the bar charts and the same min number of cases is required. SET MPRINT=no. *////////////////////. DEFINE !chart2 (minnb=!TOKENS(1) /cats=!CMDEND) /* minnb = minimum number of cases required in order to chart category value*/. /* cats = names of the categorical variables to be charted*/. COMPUTE dummy=1. !DO !cat !IN (!cats) FILTER OFF. * Sorting is necessary for the MATCH FILES command. SORT CASES BY !cat. !LET !fname=!QUOTE(!CONCAT('C:\temp\',!cat,'.SAV')) AGGREGATE /OUTFILE=!fname /PRESORTED /BREAK=!cat /nb = N(dummy). MATCH FILES /FILE=* /TABLE=!fname /BY !cat. FREQUENCY VARIABLES=!cat. LIST. COMPUTE flag=(nb>=!minnb). FILTER BY flag. GRAPH /BAR(SIMPLE)=COUNT BY !cat /MISSING=REPORT. * Delete the variable nb. MATCH FILES FILE=* /DROP=nb. !DOEND * Delete the variable dummy. MATCH FILES FILE=* /DROP=dummy. !ENDDEFINE. *///////////////////. SET MPRINT=yes. !chart2 minnb=5 cats= age prog year. EXECUTE.
The notes explaining how to use the macro
For simplicity I will only cover the first macro (the name of the macro is !chart). (The same process would be followed to use the second macro).
1.Start SPSS
2.Open (Load) your data file using the menu: File>Open>Data
3.Open a Syntax Window using the menu: File>New>Syntax
4.Select the syntax for the !chart
macro and paste it in the Syntax Window.
This is the portion of the posted answer which starts with
*////////////////////. DEFINE !chart (minnb=!TOKENS(1) /cat=!TOKENS(1))
and ends with
!ENDDEFINE. *////////////////////. SET MPRINT=yes. !chart minnb=5 cat=age.
!ENDDEFINE
command; the SET MPRINT=yes
is just to allow you to view the code in the Output window.The line !chart minnb=5 cat=age
calls the macro. The line means: do a chart using the variable age, exclude from the graph any age at which there are less than 5 persons.
6. Suppose that one of the categorical variable in you data file is language and that you want to exclude language with less than 10 persons, then replace the line
!chart minnb=5 cat=age.
by the following 3 lines:
COMPUTE dummy=1. !chart minnb=10 cat=language. EXECUTE.
COMPUTE
line is 2 lines above the DEFINE
command in the posted answer.8. Now comes the magic moment, you are ready to run the macro. To do so, use the menu: Run>All
9. Check the output window, the chart should be there.
10. If you need to do charts for variables language, region, climate. You can add the following 2 new lines
!chart minnb=10 cat=region. !chart minnb=10 cat=climate.
11. Comments: the macro
!chart
allows you to vary the minimum number of required cases for each variable.12. If the charts of many variables require the same number of minimum cases, you can use the macro !chart2
and call it using the line
!chart minnb=10 cat=age climate region.
The macro chart2 is fully commented here.
...