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

 

Hello,

I try to get the following result:

 

sasuser_8_0-1691085974393.png

 

from the following table

 

DATA SALE;

INPUT ANNEE TYPE $ VAR1 VAR2 VAR3 VAR4;

CARDS;

2020 100 52 36 98 74
2020 200 36 52 2 6 
2021 100 36 55 44 7
2021 200 65 8 24 6
2022 100 6 36 54 8
2022 200 65 8 25 4
;

RUN;

I read the following post but it's not exactly the same case:

https://communities.sas.com/t5/SAS-Procedures/Using-proc-report-to-transpose-a-table/m-p/883261 

 

Does anyone have de solution ?

 

Thank you

 

1 ACCEPTED SOLUTION

Accepted Solutions
sasuser_8
Obsidian | Level 7

Ok it works as expected.

Thank you !

View solution in original post

3 REPLIES 3
PaigeMiller
Diamond | Level 26

This should work: 

proc transpose data=sale out=sale_t;
    by annee type;
    var var1-var4;
run;

proc report data=sale_t;
    columns _name_ annee,type,col1;
    define _name_/group ' ';
    define annee/across ' ';
    define type/across ' ';
    define col1/sum ' ';
run;
ods html close;

 

As a general rule, the wide data set you start with, where repeated values of a variable (in this case VAR) are in different columns, should be avoided. The desired formatting is in data set SALE_T, this is a long (not wide) data set, where repeated values of a variable are in different rows. Using long data sets will make your programming easier, as almost all SAS PROCs are designed to work with long data sets. Please keep this in mind in your future SAS work.

--
Paige Miller
ballardw
Super User

Perhaps:

 

proc tabulate data=sale;
   class annee type;
   var var1-var4;
   table (var1-var4)*(max=' '*f=best5.),
         annee=' '*type=' '
         /row=float
   ;
run;

Proc Tabulate will allow different variables in a column. There might be an issue with what statistic you actually want given a larger data set. Sum might be appropriate as well.

The ='  ' are to suppress default labels. The *f=best5.' after the MAX or other statistic is to set the format. Default with Tabulate would be to include 2 decimal places.

The table option "row=float is clean up the row labels where the statistic has been suppressed but would leave an apparently empty column otherwise.

 

Caution with Tabulate: Default behavior is that any observation with a missing value for any of the CLASS variables is to drop the entire observation. So if you have such you need to provide examples in the data and how you expect them to be handled.

 


@sasuser_8 wrote:

 

Hello,

I try to get the following result:

 

sasuser_8_0-1691085974393.png

 

from the following table

 

DATA SALE;

INPUT ANNEE TYPE $ VAR1 VAR2 VAR3 VAR4;

CARDS;

2020 100 52 36 98 74
2020 200 36 52 2 6 
2021 100 36 55 44 7
2021 200 65 8 24 6
2022 100 6 36 54 8
2022 200 65 8 25 4
;

RUN;

I read the following post but it's not exactly the same case:

https://communities.sas.com/t5/SAS-Procedures/Using-proc-report-to-transpose-a-table/m-p/883261 

 

Does anyone have de solution ?

 

Thank you

 


 

sasuser_8
Obsidian | Level 7

Ok it works as expected.

Thank you !

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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
  • 3 replies
  • 415 views
  • 0 likes
  • 3 in conversation