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

I have a dataset with a Total column for each month data. Every time i run the report for subsequent month the new report gets merged with the previous month's report. But the Total column is coming before the 2nd months data. I want this total column to always be the last column in the report and this column should contain the sum total value from the each row. How can I do this ?

 

output i am getting is : after 1st run  metric  Jan Total 

                                    after 2nd run  metric  Jan Total Feb

                                    after 1st run  metric  Jan Total Feb Mar

 

output i want is : after 1st run          metric Jan Total

                           after 2nd run         metric Jan  Feb Total

                           after 3rd run         metric Jan  Feb  Mar Total

 

I am getting the correct summation data in the total column with each run only the arrangement I am not able to fix.

 

@cynthia

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

Rather than use month names use MONTH1-MONTH12 and then total. 

Use labels to display the month and total. 

 

Then you can add a step as follows:

 

data ordered_data;
retain Month1-Month12 Total;
set myData;
run;

A better solution is to not store your data in that format and to use a long format instead. You can use PROC REPORT/TABULATE to display it as desired but the long format will give you more control. Or store it that way in a long format with the months as numbers and Total gets 99. Then flip it using PROC TRANSPOSE as your last step that will then have everything in order.

 

Store it this way

Month Metric Value
1 Mean 24
2 Mean 24
...
12 Mean 55
99 Mean 900

Then transpose it and it should keep the order desired. But otherwise you'll have to manually specify the months in a macro variable or something because those are in an order that is not alphabetical or ordered in any fashion. 

 

View solution in original post

4 REPLIES 4
ballardw
Super User

Variable names?

Code used to "merge" data sets?

Actual example data?

Possible even how you calculate the total prior to merging is of interest.

 

90% percent of the time I see something like this a report procedure with the proper data structure handles stuff correctly. Order of columns in a data set is generally not of much importance as the computers using the data do not care, only people. And that means Report.

 

And please look at the posted information. It is quite likely that the message window on this forum already reformatted text moving the "total" column to different columns. So what we see is not what we can do much with.

PaigeMiller
Diamond | Level 26

@sasuser58 wrote:

I have a dataset with a Total column for each month data. Every time i run the report for subsequent month the new report gets merged with the previous month's report. But the Total column is coming before the 2nd months data. I want this total column to always be the last column in the report and this column should contain the sum total value from the each row. How can I do this ?

 

output i am getting is : after 1st run  metric  Jan Total 

                                    after 2nd run  metric  Jan Total Feb

                                    after 1st run  metric  Jan Total Feb Mar

 

output i want is : after 1st run          metric Jan Total

                           after 2nd run         metric Jan  Feb Total

                           after 3rd run         metric Jan  Feb  Mar Total

 

I am getting the correct summation data in the total column with each run only the arrangement I am not able to fix.


In most cases (nearly all cases), it isn't worth fighting with SAS to get the proper order of columns in a SAS data set. Why? Because any output method (PROC PRINT, PROC REPORT, PROC TABULATE), you can specify the desired order of columns there. And it's much much simpler to specify the order of columns in your output procedure than in your data set.

--
Paige Miller
Reeza
Super User

Rather than use month names use MONTH1-MONTH12 and then total. 

Use labels to display the month and total. 

 

Then you can add a step as follows:

 

data ordered_data;
retain Month1-Month12 Total;
set myData;
run;

A better solution is to not store your data in that format and to use a long format instead. You can use PROC REPORT/TABULATE to display it as desired but the long format will give you more control. Or store it that way in a long format with the months as numbers and Total gets 99. Then flip it using PROC TRANSPOSE as your last step that will then have everything in order.

 

Store it this way

Month Metric Value
1 Mean 24
2 Mean 24
...
12 Mean 55
99 Mean 900

Then transpose it and it should keep the order desired. But otherwise you'll have to manually specify the months in a macro variable or something because those are in an order that is not alphabetical or ordered in any fashion. 

 

Tom
Super User Tom
Super User

Depends on how you are combining them.

 

One easy way is to use dataset options to force SAS to define the variables in the order you want.  SAS will set the order based on when it first "sees" the variable.  For example you could have a SET statement than can never actually be executed but the compiler will still check the variables that it provides.

data want;
  if 1=0 then set old (drop=total) ;
  set old new ;
run;

 

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
  • 4 replies
  • 604 views
  • 0 likes
  • 5 in conversation