Question

Another SAS question from me (I noticed these don't come up here that often...):

I have a data set containing something like this:

Name  |  Category  |   Level  |  Score
John  |    cat1    |     1    |    80
John  |    cat1    |     2    |    70
John  |    cat1    |     3    |    10
John  |    cat2    |     1    |    60
John  |    cat2    |     2    |    95
John  |    cat2    |     3    |    43
John  |    cat2    |     4    |    28

And the output (excel format) should look like:

      |    cat1      |    cat2       |
name  | 1  |  2  | 3 | 1 | 2 | 3 | 4 | 
John  | 80 | 70  |10 |60 |95 |43 |28 |

What I do now, is using proc transpose to get the data in the right order and then proc exportto go to .xls.

This works fine, except for one thing. I cant get the second layer of subdivision to work. So right now, before my proc transpose I actually concat my category and level in my dataset (eg making it '1_cat1') and then transpose on this value, giving me the following output:

name  | 1_cat1 |  2_cat1 | 3_cat1 | 1_cat2 | 2_cat2 | 3_cat2 | 4_cat2 | 
John  |   80   |    70   |   10   |   60   |   95   |   43   |   28   |

Is there any way to get the first, desired output ?

Was it helpful?

Solution

Assuming that you're familiar with your ODS options to get it into Excel (I'm just lazily using html and saving it as .xls, but you could use tagsets etc. instead), here is a PROC REPORT solution to display the data in the format you're looking for. Check out the use of across variables in proc report. There's probably a way to suppress the column that isn't used under cat1 in the output, but I can't recall it right now.

data testData;
  infile datalines dsd delimiter='|';
  input name $ category $ level score;
  datalines;
John  |    cat1    |     1    |    80
John  |    cat1    |     2    |    70
John  |    cat1    |     3    |    10
John  |    cat2    |     1    |    60
John  |    cat2    |     2    |    95
John  |    cat2    |     3    |    43
John  |    cat2    |     4    |    28
;
run;

ods html file="C:\SomePath\MyFile.xls";

proc report
  data=testData;
  columns name category,level,score;
  define name / group;
  define category / across '';
  define level / across '';
  define score / sum '';
run;

ods html close;

OTHER TIPS

I don't think you will be able to go directly to your desired output using proc transpose since you are looking to get each category to span multiple levels. You might want to research two other procedures, REPORT and TABULATE. I believe you can do this directly from either one, but it has been years since I used these. A third option is to create an XML file with ODS in which you can control pretty much exactly how you want to output to appear, though it takes a little more effort to learn how to do this.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top