BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Scorpx
Obsidian | Level 7

Hello community and Happy New Year.

 

I have survey data that I am trying to report in a "friendly" way. Now I am stuck trying to organize four Likert scale variables into the same table, like this:

 

 1.- Not important at all2.-3.-4.- Neutral5.-6.-7.- Extremely important
RowPctNRowPctNRowPctNRowPctNRowPctNRowPctNRowPctN
v7a1.00 2.008.0022.0027.0040.00
v8a11.005.009.0020.0017.0015.0023.00

 

But if I add variables to this code, it generates a table under or beside the first variable. 

 

proc tabulate data=sd1a;
class v7 v8 ;
var v7a v8a ;
table v7a, v7*(rowpctn) v8*(rowpctn) ;
run;
 1.- Not important at all3.-4.- Neutral5.-6.-7.- Extremely important1.- Not important at all2.-3.-4.- Neutral5.-6.-7.- Extremely important
 RowPctNRowPctNRowPctNRowPctNRowPctNRowPctNRowPctNRowPctNRowPctNRowPctNRowPctNRowPctNRowPctN
v7a1.002.008.0022.0027.0040.0011.005.009.0020.0017.0015.0023.00

 

 

Is it possible to obtain a table like the first one? there should be a way, maybe including a Proc Format?

 

Any help is appreciated, thanks.

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

This is a case of you need to reshape your data somewhat to get the result that you want.

I am afraid that your data are actually character values which will complicate things because of sort orders but I am going to provide an example with numeric values in the scale to avoid having to do a LOT of extra typing.

 

Best with any question is to provide example data of what you currently have in the form of a data step so we can see immediately the characteristics of all the variables and have something to work with.

 

Here is a brief example with a 5-value.

data example;
   input v1-v3;
   label
      v1='Question 1'
      v2='Question 2'
      v3='Question 3'
   ;
datalines;
1 2 3
1 3 5
1 1 1
2 2 4
2 2 5
3 3 1
3 1 2
3 2 2
4 5 1
4 4 2
4 4 5
5 5 5
5 1 1
5 1 2
5 3 3
;

data need;
  set example;
  array vars (*) v1-v3;
  do i= 1 to dim(vars);
     name = vname(vars[i]);
     label= vlabel(vars[i]);
     value= vars[i];
     output;
  end;
  keep name value label;
run;

proc format;
value lickert
1 = 'Not important at all'
2 = 'Somewhat not imporant'
3 = 'Neutral'
4 = 'Somewhat important'
5 = 'Very important'
;
run;
proc tabulate data=need;
   class name value label;
   format value lickert.;
   table name=' ',
         value='Scale'*rowpctn=' '
   ;
   table label=' ',
         value='Scale'*rowpctn=' '
   ;
run;

The data step is to have an example of data. You can copy this into your editor and run.

The second data step reshapes the data in the form needed for proc tabulate or report to make the report you want. There needs to be a variable to hold the row heading and single variable for the column heading values associated with each row for the table you want. So I use functions Vname and Vlabel to get the text of the variable name and label into new variables using an Array to reduce repeated code. I provide a label and show how to use that to make a slightly "nicer" table in proc tabulate.

 

If there are other variables you need to include for the table make sure they are on the KEEP statement in the Need data step.

 

I also create a custom format to display my numeric scale with meaningful text for all the values.

 

You can suppress the variable or statistic name/label from appearing in the table by using an empty quoted string like Varname=' ' .

 

If you happen to have a code like 99='Not answered' in your data and only want the answer scale values to appear in this table then in the in Need data set you would exclude them with something like:

data need;
  set example;
  array vars (*) v1-v3;
  do i= 1 to dim(vars);
     if vars[i] ne 99 then do;
        name = vname(vars[i]);
        label= vlabel(vars[i]);
        value= vars[i];
        output;
     end;
  end;
  keep name value label;
run;

View solution in original post

2 REPLIES 2
ballardw
Super User

This is a case of you need to reshape your data somewhat to get the result that you want.

I am afraid that your data are actually character values which will complicate things because of sort orders but I am going to provide an example with numeric values in the scale to avoid having to do a LOT of extra typing.

 

Best with any question is to provide example data of what you currently have in the form of a data step so we can see immediately the characteristics of all the variables and have something to work with.

 

Here is a brief example with a 5-value.

data example;
   input v1-v3;
   label
      v1='Question 1'
      v2='Question 2'
      v3='Question 3'
   ;
datalines;
1 2 3
1 3 5
1 1 1
2 2 4
2 2 5
3 3 1
3 1 2
3 2 2
4 5 1
4 4 2
4 4 5
5 5 5
5 1 1
5 1 2
5 3 3
;

data need;
  set example;
  array vars (*) v1-v3;
  do i= 1 to dim(vars);
     name = vname(vars[i]);
     label= vlabel(vars[i]);
     value= vars[i];
     output;
  end;
  keep name value label;
run;

proc format;
value lickert
1 = 'Not important at all'
2 = 'Somewhat not imporant'
3 = 'Neutral'
4 = 'Somewhat important'
5 = 'Very important'
;
run;
proc tabulate data=need;
   class name value label;
   format value lickert.;
   table name=' ',
         value='Scale'*rowpctn=' '
   ;
   table label=' ',
         value='Scale'*rowpctn=' '
   ;
run;

The data step is to have an example of data. You can copy this into your editor and run.

The second data step reshapes the data in the form needed for proc tabulate or report to make the report you want. There needs to be a variable to hold the row heading and single variable for the column heading values associated with each row for the table you want. So I use functions Vname and Vlabel to get the text of the variable name and label into new variables using an Array to reduce repeated code. I provide a label and show how to use that to make a slightly "nicer" table in proc tabulate.

 

If there are other variables you need to include for the table make sure they are on the KEEP statement in the Need data step.

 

I also create a custom format to display my numeric scale with meaningful text for all the values.

 

You can suppress the variable or statistic name/label from appearing in the table by using an empty quoted string like Varname=' ' .

 

If you happen to have a code like 99='Not answered' in your data and only want the answer scale values to appear in this table then in the in Need data set you would exclude them with something like:

data need;
  set example;
  array vars (*) v1-v3;
  do i= 1 to dim(vars);
     if vars[i] ne 99 then do;
        name = vname(vars[i]);
        label= vlabel(vars[i]);
        value= vars[i];
        output;
     end;
  end;
  keep name value label;
run;

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 1132 views
  • 0 likes
  • 2 in conversation