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.
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.
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.
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.
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;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.