Programming the statistical procedures from SAS

Separating categories within a variable in a table

Reply
New Contributor
Posts: 3

Separating categories within a variable in a table

Hi,

is there a way to separate the sub levels of a variable so that the sub levels are in rows and the variables are as columns.  To illustrate what i mean, attached is a picture. First picture is in sas, second picture is what i would like to do in sas.  Apologies if this is covered else where, but i have literally searched everywhere without success.  I am guessing it is called something else since these seems like a pretty simple/common approach to creating a table.  Below is the code. Any guidance would be greatly appreciated!  thanks!

tab.png

proc tabulate data=scores;

class Q1 Q2;

table N,

Q1 Q2;

run;

Super User
Posts: 18,498

Re: Separating categories within a variable in a table

First picture can't be your data in SAS, that's the report. You need to show what your actual data looks like.

Unless you have them backwards?

The word you're looking for is transpose though:

SAS Learning Module: How to reshape data wide to long using proc transpose

SAS Learning Module: Reshaping wide to long using a data step

New Contributor
Posts: 3

Re: Separating categories within a variable in a table

Apologies, yes the data looks like in the picture below

q.png

Super User
Posts: 18,498

Re: Separating categories within a variable in a table

TThat's already your second picture? Or do you need it summarized/aggregated somehow?

New Contributor
Posts: 3

Re: Separating categories within a variable in a table

Yup exactly, I need the raw data (pic 3) summed like it is in pic 1 and pic 2.   the first picture shows the counts for the subcategories within a variable (categorical) from sas, pic 2 is the format i would like it in.  So for example, the possible choices for Q1 are 1 - 5.  8 people responded 1, 7 people ressponded 2 etc.  So It shows the counts for each choice.  The third picture is just the raw data.  For now what i do is get counts, export it to excel and create pivot tables to achieve this.  But Im wondering if htere is a quicker way to do this.

Super User
Posts: 18,498

Re: Separating categories within a variable in a table

Yes, but the data you showed only shows a portion of your problem. It would help if you would illustrate the full problem with sample data in the text here so we can use it directly rather than type it out. The general solution will be along the lines of query, with a specific group by clause and then sum or count the values as required. That will create the data set and then you can easily print out that data set using the LIST task

Occasional Contributor
Posts: 7

Re: Separating categories within a variable in a table

You are wanting two univariate summaries.

One way is to put all the results into a single variable:


data allscores;  * or use proc transpose;

   SET scores;

   q='q1'; result=q1; output;

   q='q2'; result=q2; output;

proc tabulate data=allscores;

  class q result;

  table result,q ;

  run;

Ask a Question
Discussion stats
  • 6 replies
  • 338 views
  • 0 likes
  • 3 in conversation