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

I have this hypothetical dataset:

 

MRN     Height      Weight 
12345    65           22
12345    62           22
77111    60           33
77111    70           28
77111    58           35

Ultimately, I want to convert from long to wide, with variables like Height1, Height2, Weight1, Weight2, etc. using PROC TRANSPOSE. However, I need to create a "time" variable that indicates when the height and weight were measured before I can do this. I was thinking of counting the number of rows by MRN and adding a new column that goes from 1 to the number of rows by MRN. I need the dataset to look like this:

 

MRN     Height      Weight       Time
12345    65           22          1
12345    62           22          2
77111    60           33          1
77111    70           28          2
77111    58           35          3

 

Any suggestions would be greatly appreciated.

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Do it in a data step:

data numbered;
set have;
by mrn;
if first.mrn
then time = 1;
else time + 1;
run;

Depending on how your dataset is ordered with regards to mrn, you will have to either sort first or use the notsorted option in the by statement.

View solution in original post

4 REPLIES 4
Kurt_Bremser
Super User

Do it in a data step:

data numbered;
set have;
by mrn;
if first.mrn
then time = 1;
else time + 1;
run;

Depending on how your dataset is ordered with regards to mrn, you will have to either sort first or use the notsorted option in the by statement.

corkee
Calcite | Level 5
This is perfect. Thanks so much.
HB
Barite | Level 11 HB
Barite | Level 11

Is there a reason you want to denormalize your data?  It will make it harder to work with.  If you want to find things like difference in weight over time I guarantee the format it is in now (height, weight, time) will be a lot easier to work with than weight1, time1, weight2, time2, weight3, time3, weight4, time4, etc.  

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Its rarely a good idea to transpose data, it makes it harder to work with the data.  Anyhows, you don't need time, or tranpose, you can simply do it with arrays (I fix the number of elements here to 3, but you could take a max and use that rather):

data want (keep=mrn height: weight:);
  set have (rename=(height=_height weight=_weight));
  array height{3};
  array weight{3};
  by mrn;
  retain ind;
  if first.mrn then ind=1;
  else ind=ind+1;
  height{ind}=_height;
  weight{ind}=_weight;
  if last.mrn then output;
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
  • 2365 views
  • 0 likes
  • 4 in conversation