DATA Step, Macro, Functions and more

PROC SQL - Counting by ID

Accepted Solution Solved
Reply
Contributor
Posts: 29
Accepted Solution

PROC SQL - Counting by ID

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.


Accepted Solutions
Solution
‎12-01-2017 11:06 AM
Super User
Posts: 9,868

Re: PROC SQL - Counting by ID

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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code

View solution in original post


All Replies
Solution
‎12-01-2017 11:06 AM
Super User
Posts: 9,868

Re: PROC SQL - Counting by ID

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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Contributor
Posts: 29

Re: PROC SQL - Counting by ID

Posted in reply to KurtBremser
This is perfect. Thanks so much.
Super Contributor
Super Contributor
Posts: 266

Re: PROC SQL - Counting by ID

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.  

Super User
Super User
Posts: 9,397

Re: PROC SQL - Counting by ID

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;
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 180 views
  • 0 likes
  • 4 in conversation