Hey everyone.
Need help with transposing data from long to wide. Here is the data I have:
pump_number | adj_cycle_number | cycle_up_slope | cycle_down_slope | target |
11 | 1 | 1843.61 | -3894.3 | fail |
11 | 2 | 2055.37 | -4053.6 | fail |
11 | 3 | 2154.83 | -4212.41 | fail |
12 | 1 | 2132.037 | -2704.71 | pass |
12 | 2 | 2139.293 | -2855.92 | pass |
12 | 3 | 2232.83 | -4663.27 | pass |
This is what I need:
pump_number | cycle_up_slope1 | cycle_up_slope2 | cycle_up_slope3 | cycle_down_slope1 | cycle_down_slope2 | cycle_down_slope3 | target |
11 | 1843.61 | 2055.37 | 2154.83 | -3894.3 | -4053.6 | -4212.41 | fail |
12 | 2132.037 | 2139.293 | 2232.83 | -2704.71 | -2855.92 | -4663.27 | pass |
Can you help?
Thanks!
Dipu
data have;
input pump_number adj_cycle_number cycle_up_slope cycle_down_slope target $;
cards;
11 1 1843.61 -3894.3 fail
11 2 2055.37 -4053.6 fail
11 3 2154.83 -4212.41 fail
12 1 2132.037 -2704.71 pass
12 2 2139.293 -2855.92 pass
12 3 2232.83 -4663.27 pass
;
proc transpose data=have out=_have;
by pump_number adj_cycle_number target;
var cycle_up_slope cycle_down_slope;
run;
proc transpose data=_have out=want(drop=_name_);
by pump_number target;
var col1;
id _name_ adj_cycle_number;
run;
Transposing data tutorials:
Long to Wide:
https://stats.idre.ucla.edu/sas/modules/how-to-reshape-data-long-to-wide-using-proc-transpose/
https://stats.idre.ucla.edu/sas/modules/reshaping-data-long-to-wide-using-the-data-step/
Both of these have examples for exactly your situation.
The data step lets you do it in one step, the transpose is multiple steps but easier to understand for beginners in my opinion.
If you have issues getting it to work feel free to post your code.
@DipuRahman wrote:
Hey everyone.
Need help with transposing data from long to wide. Here is the data I have:
pump_number adj_cycle_number cycle_up_slope cycle_down_slope target 11 1 1843.61 -3894.3 fail 11 2 2055.37 -4053.6 fail 11 3 2154.83 -4212.41 fail 12 1 2132.037 -2704.71 pass 12 2 2139.293 -2855.92 pass 12 3 2232.83 -4663.27 pass
This is what I need:
pump_number cycle_up_slope1 cycle_up_slope2 cycle_up_slope3 cycle_down_slope1 cycle_down_slope2 cycle_down_slope3 target 11 1843.61 2055.37 2154.83 -3894.3 -4053.6 -4212.41 fail 12 2132.037 2139.293 2232.83 -2704.71 -2855.92 -4663.27 pass
Can you help?
Thanks!
Dipu
data have;
input pump_number adj_cycle_number cycle_up_slope cycle_down_slope target $;
cards;
11 1 1843.61 -3894.3 fail
11 2 2055.37 -4053.6 fail
11 3 2154.83 -4212.41 fail
12 1 2132.037 -2704.71 pass
12 2 2139.293 -2855.92 pass
12 3 2232.83 -4663.27 pass
;
proc transpose data=have out=_have;
by pump_number adj_cycle_number target;
var cycle_up_slope cycle_down_slope;
run;
proc transpose data=_have out=want(drop=_name_);
by pump_number target;
var col1;
id _name_ adj_cycle_number;
run;
Thanks you
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.