Taylrl
Taylrl

Reputation: 3919

SAS proc transpose without needing to sort the data

I am attempting to do a relatively basic [proc transpose] however the issue I am having is that the function is expecting is expecting the data to be sorted. This is fine for one of the variables I am using however I cant sort the other. The data I am working with looks like this;

data have;
    input SESSION $ Activity $;
    datalines;
AAAA    Activity1
AAAA    Activity1
AAAA    Action1
AAAA    Action1
AAAA    Activity2
AAAA    Activity2
AAAA    Action1
BBBB    Action1
BBBB    Action3
BBBB    Action3
BBBB    Activity2
BBBB    Activity2
CCCC    Activity1
CCCC    Activity1
CCCC    Action2
CCCC    Action2
CCCC    Action4
CCCC    Action1
CCCC    Activity1
;
run;

I am trying however to make the data look like this, so that for each session the different actions/activities go horizontally;

data have;
input Session $ Label $ Part"x" $;
datalines;
AAAA    Journey    Activity1    Activity1    Action1    Action1    Activity2    Activity2    Action1
BBBB    Journey    Action1      Action3      Activity2  Activity2
CCCC    Journey    Activity1    Activity1    Action2    Action2    Action4    Action1    Activity1

I have been playing around with the following code however the [proc transpose] function requires the data to be sorted, which the 'Activity' in this case can't be. I imagine I need to use the [notsorted] function but I don't know where to put it. Whilst I know this isn't correct I have the following;

proc sort data=custs.tenmay_pre_transpose
        out=custs.tenmay_trans_srt ;
by SESSIONID Activty_Category notsorted;
run;

proc transpose data=custs.tenmay_trans_srt
                out= custs.tenmay_traspose
                name=Journey;
by SESSIONID;
run; 

Upvotes: 0

Views: 2634

Answers (1)

Joe
Joe

Reputation: 63424

Activity does not need to be sorted. Only by variable(s) need to be sorted.

proc sort data=have;
  by session;
run;

proc transpose data=have
                out= want
                name=Journey;
by SESSION;
var activity;
run; 

That gets you what you want, I think (I'm not sure where Journey is supposed to fit in there, but I left it as you have in the code - it is the name of the variable storing the transposed variable's name).

Upvotes: 3

Related Questions