DATA Step, Macro, Functions and more

Create variables each contains ordered values from existing variable within by group

Reply
Occasional Contributor
Posts: 5

Create variables each contains ordered values from existing variable within by group

Hi all,

 

I have a data set that contains such variables:

 

idtimedrug
12/1/2010A
13/1/2010B
14/1/2010B
15/1/2010C
23/2/2010B
24/2/2010C
35/4/2010A
36/4/2010A
37/4/2010C

I wanted to create variables of drug1 drug2 drug3 drug4...etc which stores values of variable drug in a chronological order within per by group, so the output looks like:

idtimedrugdrug1drug2drug3
12/1/2010AABC
13/1/2010BABC
14/1/2010BABC
15/1/2010CABC
23/2/2010BBC.
24/2/2010CBC.
35/4/2010AAC.
36/4/2010AAC.
37/4/2010CAC.

 

 

I can only think of the "sort, retain, carry down non-missing value" method. Is there a more efficient way to do this?

 

Thank you!

Super User
Super User
Posts: 9,599

Re: Create variables each contains ordered values from existing variable within by group

Posted in reply to zzfsimona

Sorry your going to have to explain that a bit clearer.  In the presented test data (which is not in a datastep!!!), there are four rows for id 1, are you saying that you want the sequence from all the id sorted, transposed and then merged back to the original data?  If so then - and note this is not tested as no test data in a datastep provided:

proc sort data=have out=list nodupkey;
  by id drug;
run;
proc tranpose data=list out=llist;
  by id;
  var drug;
run;
data want;
  merge have llist;
  by id;
run;
Super User
Posts: 6,785

Re: Create variables each contains ordered values from existing variable within by group

Posted in reply to zzfsimona

I think PROC TRANSPOSE is the right general direction, but I would make a few changes.  First, since you indicated chronological order, the sorting order would be:

 

proc sort data=have;

by id time;

run;

 

Note that this assumes your TIME values are true SAS dates, not text.

 

Then get the variable names  you would like:

 

proc transpose data=have prefix=drug out=druglist (drop=_name_);

by id;

var drug;

run;

 

Finally, merge as was suggested:

 

data want;

merge have druglist;

by id;

run;

 

Occasional Contributor
Posts: 5

Re: Create variables each contains ordered values from existing variable within by group

Posted in reply to Astounding

Thank you for the suggestion. What if I wanted the DISTINCT values transposed?

For example for id=1, now they have

drug1 drug2 drug3 drug4

A B B C

I want the distinct values so:

drug1 drug2 drug3

A B C

 

Thanks!

Valued Guide
Posts: 580

Re: Create variables each contains ordered values from existing variable within by group

[ Edited ]
Posted in reply to zzfsimona

Untested: use proc sort with nodupkey option before transposing:

proc sort data=have out=singles nodupkey;
  by id drug;
run;

Wait .... that doesn't work because you need the dates of the eliminated duplicates.

Super User
Super User
Posts: 9,599

Re: Create variables each contains ordered values from existing variable within by group

Posted in reply to zzfsimona

Did my post not do this for you?

Super User
Posts: 6,785

Re: Create variables each contains ordered values from existing variable within by group

Posted in reply to zzfsimona

Then you need to further define the outcome.  You asked for chronological order.  What if the chronological order is ABCB.  Do you want ABC as the result or ACB as the result?  That's just a simple example.  You really need a couple of comprehensive rules about what the order should be.

Ask a Question
Discussion stats
  • 6 replies
  • 89 views
  • 0 likes
  • 4 in conversation