BookmarkSubscribeRSS Feed
Olga3
Calcite | Level 5

Hi All,

 

I need to produce a report where variables have several categories. It looks like this:

VariableCategoryValue (counts)
ResidenceUrban11
 Rural10
EducationNone10
 Primary6
 Secondary2
 Tertuary3

So I need to stack rows for Residence, Education, etc. For example, for Residence, the Variable column will have one stacked row, while Categories and Values will have two rows (urban and rural). For Education, the Variable column will also have one stacked row, while Categories and Values will have four rows. I know that you can stack columns in PROC TEMPLATE, but I need to stack rows. I can produce a SAS dataset that has this structure, but I wonder if there is a more efficient way to do it - using PROC TEMPLATE or PROC REPORT. Many thanks for your help!

 

Best,

-Olga

 

 

4 REPLIES 4
ballardw
Super User

Please look closely at the way that your "example" appears and see if that is what you actually want. I suspect that the forum might have moved somethings from the second column to the first.

If not, that is a very hard to read layout.

If the columns were shifted for some values then please show the expected layout. Best would be to provide some actual data and the layout of the expected result using that data.

 

Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the </> icon or attached as text to show exactly what you have and that we can test code against.

 

 

Olga3
Calcite | Level 5
Thank you ballardw. I corrected my example.
ballardw
Super User

Are all of the variables of interest character or do you have a mix of character and numeric? There are some approaches that work with one type of data that don't work with mixed data.

 

Here is one way with only character data which reshapes the data.

data need;
   set sashelp.shoes;
   array v region product;
   /* length for name based on longest name of variables
      value based on longest length of any of the variables
   */
   length name $ 20 value $15;
   do i = 1 to dim(V);
      name = vname(v[i]);
      value = v[i];
      output;
   end;
   keep name value;
run;

proc tabulate data=need;
   class name value;
   table name=''*value='',
         n='Count'
   ;
run;
   
Olga3
Calcite | Level 5

Thank you! I already have a code that produces a SAS dataset that is shaped the way I need the output to be. What I asked about was if there is a way to display the results without extra coding. For stacked columns, proc report and proc tabulate have this option. But I haven't found a similar option for rows.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 874 views
  • 0 likes
  • 2 in conversation