# collapse the observations

User Forumcollapse the observations

Hi Aptech,

I have a question like this, if I have a matrix in the form of

`X=[A 2, B 3, C 5, B 4]`

I want to produce a matrix like this

`Y=[A 2 NA,`
`   B 3  4,`
`   C 5 NA]`

Could you please tell me how I can get this.

Thank you very much !

aptech Staff answered 2 years ago

Below is a procedure that should work. I would encourage you to step through this with the debugger to help you to understand how it works.

```X = { A 2, B 3, C 5, B 4 };
X_new = collapseObs(X);

proc (1) = collapseObs(X);
local U, mask, max_len, Y, Y_row;

//Find the unique identifiers
//from column 1
U = unique(X[.,1], 0);

//to a row from 'U', above

//Find out the maximum number of
//elements corresponding to any
//of the unique values in 'U'

//Fill a matrix full of missing values
Y = reshape(error(0), rows(U), max_len + 1);

//Overlay unique values from 'U'
//into first column
Y[.,1] = U;

//Fill in observations
for i(1, rows(Y), 1);
//Find observations for the
//i'th element of 'U'
Y[i, 2:cols(Y_row)+1] = Y_row;
endfor;

retp(Y);
endp;
```

• ### Aptech Systems, Inc. Worldwide Headquarters

Aptech Systems, Inc.
2350 East Germann Road, Suite #21
Chandler, AZ 85286

Phone: 360.886.7100
FAX: 360.886.8922

• ### Training & Events

Want more guidance while learning about the full functionality of GAUSS and its capabilities? Get in touch for in-person training or browse additional references below.

• ### Tutorials

Step-by-step, informative lessons for those who want to dive into GAUSS and achieve their goals, fast.

• ### Have a Specific Question?

Get a real answer from a real person

• Need Support?
• ### Support Plans

Premier Support and Platinum Premier Support are annually renewable membership programs that provide you with important benefits including technical support, product maintenance, and substantial cost-saving features for your GAUSS System or the GAUSS Engine.

• ### User Forums

Join our community to see why our users are considered some of the most active and helpful in the industry!