BookmarkSubscribeRSS Feed
PhillipSherlock
Obsidian | Level 7

I am working with a transaction type data set where there are multiple observations for individual children.  Each child has a unique "child_id" and there are multiple observations, for which the child has an associated value for the variable TREATMENT.  There may or may not be repeats for the values of TREATMENT.


There are around 80,000 children. There an indefinite number of TREATMENT paths based on around 30 TREATMENT types.


The following is similar to what the data currently looks like:


Child          Treatment

1                    A

1                    C

1                    C

1                    D    

1                    B

2                    B

2                    B

2                    A

2                    B


The following is just a brief example of what I would like the data to look like. The TREATMENT should only increment when the treatment type has changed.

Child    Treatment_1      Treatment_2     Treatment_3     Treatment_4     Treatment_n

   1            A                         C                     D                    B                      

   2            B                         A                     B                                       

                                              

Thank you for your insight

2 REPLIES 2
art297
Opal | Level 21

data need;

  set have;

  by child treatment notsorted;

  if last.treatment then output;

run;

proc transpose data=need out=want prefix=treatment_;

  by child;

  var treatment;

run;

KachiM
Rhodochrosite | Level 12

Another Solution with Data Step.

You need to have the data set, HAVE. to be pre-sorted by Child.

The array size is taken as 4 but can be extended as desired.

data want;

array t

  • $1 Treatment_1 - Treatment_4;
  • do until(last.Child);

       set have;

       by Child;

       if first.Child then i = 1; t = Treatment; i + 1; end;

       else if Treatment ^= t[i - 1] then do; t = Treatment; i + 1; end;

    end;

    drop i Treatment;

    run;

    sas-innovate-2024.png

    Don't miss out on SAS Innovate - Register now for the FREE Livestream!

    Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

     

    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
    • 2 replies
    • 802 views
    • 3 likes
    • 3 in conversation