BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
raja777pharma
Fluorite | Level 6

Hello ,

 

I want to transpose data based on values .

 

My data is as per below data set:X and would like to need data as per below data set :Y

 

Please help to programing this one

 

Data : X

 

SUBJID VISITC_STATUSC_DATEC_01C_02C_03D_STATUSD_DATED_01D_02D_03
10001FIRSTYES11-Jan-19X  YES11-Jan-19 X 
10002FIRSTYES12-Feb-19X  YES12-Feb-19 X 
10003FIRSTYES09-Dec-19X  YES09-Jan-18 X 
10004SECONDYES11-Jan-20X  YES11-Jan-19 X 
10005SECONDYES12-Feb-20X  YES12-Feb-19 X 
10006SECONDYES13-Mar-20X  YES09-Jan-18 X 

 

Data : Y

 

SUBJID VISITSTATUSDATE_01_02_03_
10001FIRSTC_YES11-Jan-19C_01_X
10002FIRSTC_YES12-Feb-19C_01_X
10003FIRSTC_YES09-Dec-19C_01_X
10004SECONDC_YES11-Jan-20C_01_X
10005SECONDC_YES12-Feb-20C_01_X
10006SECONDC_YES13-Mar-20C_01_X
10001FIRSTD_YES11-Jan-19D_02_X
10002FIRSTD_YES12-Feb-19D_02_X
10003FIRSTD_YES09-Dec-19D_02_X
10004SECONDD_YES11-Jan-20D_02_X
10005SECONDD_YES12-Feb-20D_02_X
10006SECONDD_YES13-Mar-20D_02_X
1 ACCEPTED SOLUTION

Accepted Solutions
hashman
Ammonite | Level 13

@raja777pharma:

When you need to process data lists falling in the same pattern in an identical manner, arrays become your friends. For example:

data have ;                                                                                                                                                                                                                                                     
  input SUBJID $ VISIT $ C_STATUS $ C_DATE:$9. (C_01-C_03) (:$1.)                                                                                                                                                                                               
                         D_STATUS $ D_DATE:$9. (D_01-D_03) (:$1.)                                                                                                                                                                                               
  ;                                                                                                                                                                                                                                                             
  cards ;                                                                                                                                                                                                                                                       
10001  FIRST   YES  11-Jan-19  X  .  .  YES  11-Jan-19  .  X  .                                                                                                                                                                                                 
10002  FIRST   YES  12-Feb-19  X  .  .  YES  12-Feb-19  .  X  .                                                                                                                                                                                                 
10003  FIRST   YES  09-Dec-19  X  .  .  YES  09-Jan-18  .  X  .                                                                                                                                                                                                 
10004  SECOND  YES  11-Jan-20  X  .  .  YES  11-Jan-19  .  X  .                                                                                                                                                                                                 
10005  SECOND  YES  12-Feb-20  X  .  .  YES  12-Feb-19  .  X  .                                                                                                                                                                                                 
10006  SECOND  YES  13-Mar-20  X  .  .  YES  09-Jan-18  .  X  .                                                                                                                                                                                                 
;                                                                                                                                                                                                                                                               
data w (keep = subjid visit status date _01_02_03_) / view = w ;                                                                                                                                                                                                
  set have ;                                                                                                                                                                                                                                                    
  array st (i) c_status d_status ;                                                                                                                                                                                                                              
  array dt (i) c_date   d_date ;                                                                                                                                                                                                                                
  array cd [2, 3] c_0: d_0: ;                                                                                                                                                                                                                                   
  do over st ;                                                                                                                                                                                                                                                  
    pfx = char (vname (st), 1) ;                                                                                                                                                                                                                                
    status = pfx || "_" || st ;                                                                                                                                                                                                                                 
    date   = dt ;                                                                                                                                                                                                                                               
    do j = 1 to dim (cd, 2) ;                                                                                                                                                                                                                                   
      if cmiss (cd[i,j]) then continue ;                                                                                                                                                                                                                        
      _01_02_03_ = pfx || "_" || put (j, z2.) || "_" || cd[i,j] ;                                                                                                                                                                                               
      output ;                                                                                                                                                                                                                                                  
    end ;                                                                                                                                                                                                                                                       
  end ;                                                                                                                                                                                                                                                         
run ;                                                                                                                                                                                                                                                           
                                                                                                                                                                                                                                                                
proc sort data = w out = want ;                                                                                                                                                                                                                                 
  by status subjid ;                                                                                                                                                                                                                                            
run ;                                 

Kind regards

Paul D. 

View solution in original post

3 REPLIES 3
PaigeMiller
Diamond | Level 26
data wnat;
    set have;
    status=cats('C_',c_status);
    date=c_date;
    if c_01='X' then _01_02_03='C_01_X';
    else if c_02='X' then _01_02_03='C_02_X';
    else if c_03='X' then _01_02_03='C_03_X';
    output;
    /* then you repeat the above 6 lines for D, you do the typing, I'm lazy */
run;

You have to sort the resulting data set to get the exact output data set that you asked for.

--
Paige Miller
ErikLund_Jensen
Rhodochrosite | Level 12

Hi @raja777pharma 

 

If more than one set of observations is contained in the same record, one would normally expect all informations of a given set to be kept together, when the set is split out in individual records.

 

In orher words, is your example output correct, or should the second set of observations, where STATUS is taken from D_STATUS, also take DATE from D_DATE instead of C_DATE, so your last output record would have DATE = 09-Jan-18?

hashman
Ammonite | Level 13

@raja777pharma:

When you need to process data lists falling in the same pattern in an identical manner, arrays become your friends. For example:

data have ;                                                                                                                                                                                                                                                     
  input SUBJID $ VISIT $ C_STATUS $ C_DATE:$9. (C_01-C_03) (:$1.)                                                                                                                                                                                               
                         D_STATUS $ D_DATE:$9. (D_01-D_03) (:$1.)                                                                                                                                                                                               
  ;                                                                                                                                                                                                                                                             
  cards ;                                                                                                                                                                                                                                                       
10001  FIRST   YES  11-Jan-19  X  .  .  YES  11-Jan-19  .  X  .                                                                                                                                                                                                 
10002  FIRST   YES  12-Feb-19  X  .  .  YES  12-Feb-19  .  X  .                                                                                                                                                                                                 
10003  FIRST   YES  09-Dec-19  X  .  .  YES  09-Jan-18  .  X  .                                                                                                                                                                                                 
10004  SECOND  YES  11-Jan-20  X  .  .  YES  11-Jan-19  .  X  .                                                                                                                                                                                                 
10005  SECOND  YES  12-Feb-20  X  .  .  YES  12-Feb-19  .  X  .                                                                                                                                                                                                 
10006  SECOND  YES  13-Mar-20  X  .  .  YES  09-Jan-18  .  X  .                                                                                                                                                                                                 
;                                                                                                                                                                                                                                                               
data w (keep = subjid visit status date _01_02_03_) / view = w ;                                                                                                                                                                                                
  set have ;                                                                                                                                                                                                                                                    
  array st (i) c_status d_status ;                                                                                                                                                                                                                              
  array dt (i) c_date   d_date ;                                                                                                                                                                                                                                
  array cd [2, 3] c_0: d_0: ;                                                                                                                                                                                                                                   
  do over st ;                                                                                                                                                                                                                                                  
    pfx = char (vname (st), 1) ;                                                                                                                                                                                                                                
    status = pfx || "_" || st ;                                                                                                                                                                                                                                 
    date   = dt ;                                                                                                                                                                                                                                               
    do j = 1 to dim (cd, 2) ;                                                                                                                                                                                                                                   
      if cmiss (cd[i,j]) then continue ;                                                                                                                                                                                                                        
      _01_02_03_ = pfx || "_" || put (j, z2.) || "_" || cd[i,j] ;                                                                                                                                                                                               
      output ;                                                                                                                                                                                                                                                  
    end ;                                                                                                                                                                                                                                                       
  end ;                                                                                                                                                                                                                                                         
run ;                                                                                                                                                                                                                                                           
                                                                                                                                                                                                                                                                
proc sort data = w out = want ;                                                                                                                                                                                                                                 
  by status subjid ;                                                                                                                                                                                                                                            
run ;                                 

Kind regards

Paul D. 

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 629 views
  • 1 like
  • 4 in conversation