Condense observations

Occasional Contributor
Posts: 10

Condense observations

[ Edited ]

hi all,

i am not getting this please resolve this i want this obesrvations into one oservation how to get this

data like this,

pid        subj         date1                date2               date3              date4               compliant1    comliant2      compaint3      complaint4
1            10      12-01-2016      12-01-2016      12-01-2016    12-01-2016              same                                 same2
1            10      12-01-2016      12-01-2016       12-01-2016                                                        same                                    same2

how to get this one into one observation please give me your suggestion please i want want like this


pid        subj         date1                date2               date3              date4               compliant1    comliant2      compaint3      complaint4
1            10      12-01-2016      12-01-2016      12-01-2016    12-01-2016              same            same             same2           same2

Super User
Posts: 5,382

Re: Condense observations

With just one example, it's hard to sugest a solution.

What variables can be missing?

Is there a chance for both observation have different non-missing values in the same variable?

What is the unique key - how do you define whicj row to condense?

Data never sleeps
Posts: 653

Re: Condense observations

Are the two observations always either missing or equal? Smiley Happy


Please provide more information

Valued Guide
Posts: 947

Re: Condense observations

[ Edited ]

You have two sets of variables, grouping variables (PID & SUBJ) and other vars (2 sets of 4 vars).   It looks to me like you want to examine the sequence of all records in a given PID/SUBJ group, and the generate one record with the latest non-missing value of the other vars.


If so then this code (untested) should do what you want. 



  1. The coalesce function take the list of numeric arguments from left to right, and returns the first one that is non-missing. COALESCEC does the same for character arguments.
  2. The TMP arrays get the cumulative results as the each record is read in.  At the end-of-group, the orignal vars get data back from the TMP arrays.
  3. When you see statments like "array date {4};" with no variable names after the dimension specification, it tells SAS to use vriables named DATE1, DATE2, DATE3, and DATE4 as array references date{1},date{2],date{3}, and date{4}, respectively.




data want (drop=tmp: I);

  array tmpdate      {4} ;  /* Temporary storage of non-missing dates */
  array tmpcomplaint {4} $20;

  do until (last.subj);
    set have;
    by pid subj;
    array date {4};
    array complaint{4};

    do I=1 to 4;
       tmpdate{I}     =coalesce(date{I},tmpdate{I});
  do I=1 to 4;



Occasional Contributor
Posts: 10

Re: Condense observations

THank you So Much all.

Ask a Question
Discussion stats
  • 4 replies
  • 1 like
  • 4 in conversation