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

i need the output as below,can anyone help me

 


id profit1 profit2 profit3 profit4 revenue1 revenue2 revenue3 revenue4
1    2         5      7    9           10       20       30       40
2    3         8      .    .           3         8        .    

.

 

Data I have

 


Data have;
input id serial Revenue profit;
cards;
1 1 10 2
1 2 20 5
1 3 30 7
1 4 40 9
2 1 5 3
2 2 10 8
2 3 . .
2 4 . .
;
Run;
proc print; run;

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Seems like a simple application of using ARRAYs.

Let's rename the original variables so we can use revenue and profit as the names of the arrays.

data want;
  do until(last.id);
    set have(rename=(profit=_profit revenue=_revenue));
    by id;
    array profit [4];
    array revenue [4];
    profit[serial] = _profit;
    revenue[serial] = _revenue;
  end;
  drop serial _profit _revenue;
run;

Result

Obs    id    profit1    profit2    profit3    profit4    revenue1    revenue2    revenue3    revenue4

 1      1       2          5          7          9          10          20          30          40
 2      2       3          8          .          .           5          10           .           .

View solution in original post

2 REPLIES 2
Tom
Super User Tom
Super User

Seems like a simple application of using ARRAYs.

Let's rename the original variables so we can use revenue and profit as the names of the arrays.

data want;
  do until(last.id);
    set have(rename=(profit=_profit revenue=_revenue));
    by id;
    array profit [4];
    array revenue [4];
    profit[serial] = _profit;
    revenue[serial] = _revenue;
  end;
  drop serial _profit _revenue;
run;

Result

Obs    id    profit1    profit2    profit3    profit4    revenue1    revenue2    revenue3    revenue4

 1      1       2          5          7          9          10          20          30          40
 2      2       3          8          .          .           5          10           .           .
PaigeMiller
Diamond | Level 26

Usually there is little benefit to transposing long to wide. If you want to create a report (as opposed to a SAS data set) in this wide format that you want, leave the data as it is in the long arrangement, and use PROC REPORT.

 

proc report data=have;
    columns id profit,(" " serial) revenue,(" " serial) ;
    define id/group 'ID';
    define serial/across " ";
    define revenue/sum 'Revenue';
    define profit/sum 'Profit';
run;

 

There are plenty of formatting capabilites to make the above PROC REPORT output look nicer if needed.

--
Paige Miller

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

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 2 replies
  • 879 views
  • 2 likes
  • 3 in conversation