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

Hello,

 

I have the following table:

ProductYearCostRevenue
A2020100110
A202012090
B2020220250
B2020230250

 

I want to create a new table, which replaces the numerical Columns (Cost & Revenue) with two new Columns: text "Metric Name" &  Numerical "Metric Value" and summarize them as below;

 

ProductYearMetric NameMetric Value
A2020Cost220
A2020Revenue200
B2020Cost450
B2020Revenue500

 

Does anybody know how to do this ? 

 

Thank you very much,

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

There you go. Seems easier to report directly though.

 

data have;
input Product $ Year Cost Revenue;
datalines;
A 2020 100 110
A 2020 120 90
B 2020 220 250
B 2020 230 250
;

data want(keep=Product Year Metric_Name Metric_Value);
   c = 0; r = 0;

   do until (last.Product);
      set have;
      by Product;
      c + Cost;
      r + Revenue;
   end;

   Metric_Name  = "Cost   ";
   Metric_Value = c;
   output;

   Metric_Name  = "Revenue";
   Metric_Value = r;
   output;
run;

View solution in original post

4 REPLIES 4
PeterClemmensen
Tourmaline | Level 20

Is this for reporting purposes or do you want a SAS data set like this?

VCucu
Obsidian | Level 7
i would like a dataset like this. will use it for reporting in a later step with proc report. thank you
PeterClemmensen
Tourmaline | Level 20

There you go. Seems easier to report directly though.

 

data have;
input Product $ Year Cost Revenue;
datalines;
A 2020 100 110
A 2020 120 90
B 2020 220 250
B 2020 230 250
;

data want(keep=Product Year Metric_Name Metric_Value);
   c = 0; r = 0;

   do until (last.Product);
      set have;
      by Product;
      c + Cost;
      r + Revenue;
   end;

   Metric_Name  = "Cost   ";
   Metric_Value = c;
   output;

   Metric_Name  = "Revenue";
   Metric_Value = r;
   output;
run;
Kurt_Bremser
Super User

If year were character, this would be the most simple code:

proc summary data=have nway;
by product year;
var _numeric_;
output out=sum (drop=_type_ _freq_) sum()=;
run;

proc transpose data=sum out=want (rename=(_name_=metric_name col1=metric_value));
by product year;
var _numeric_;
run;

If, OTOH, you positively know that you have only cost and revenue, then a data step does it it in one operation:

data want;
set have;
by product year;
if first.year
then do;
  _cost = cost;
  _revenue = revenue;
end;
else do;
  _cost + cost;
  _revenue + revenue;
end;
if last.year;
metric_name = 'cost   ';
metric_value = _cost;
output;
metric_name = 'revenue';
metric_value = _revenue;
output;
keep product year metric_name metric_value;
run;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 950 views
  • 4 likes
  • 3 in conversation