BookmarkSubscribeRSS Feed
zzfsimona
Fluorite | Level 6

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!

6 REPLIES 6
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;
Astounding
PROC Star

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;

 

zzfsimona
Fluorite | Level 6

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!

andreas_lds
Jade | Level 19

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.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Did my post not do this for you?

Astounding
PROC Star

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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 6 replies
  • 742 views
  • 0 likes
  • 4 in conversation