Help using Base SAS procedures

How to read this matrix

Reply
N/A
Posts: 0

How to read this matrix

I don't remember which function to use to read a matrix like following into the format I want:

matrix:

y1 y2 y3
x1 z11 z12 z13
x2 z21 z22 z23

desired format

x1 y1 z11
x1 y2 z12
x1 y3 z13
x2 y1 z21
x2 y2 z22
x2 y3 z23

Thanks for any help in advance!
Frequent Contributor
Posts: 82

Re: How to read this matrix

Posted in reply to deleted_user
Hi,

Could you please clarify what input data do you have?

Is it like a matrix? (If so, is the 4th observation in 1st row missing so that it would be (3X4) matrix. Or do you just want to make data set like below from no initial data?
N/A
Posts: 0

Re: How to read this matrix

the data are saved in .csv files. "X's" are company names, "Y's" are dates, and "Z's" are the value of a variable for company X on date Y. I have quite a number of these files, each recording one specific variable, and each containing a large number of entries.

the first raw is missing one (the first) observation.

Thanks.
PROC Star
Posts: 1,759

Re: How to read this matrix

Posted in reply to deleted_user
This isn't a place where people will write code to specs for free, especially if the specs are given piecemeal.
It is however a place where people will give you pointers so you can learn.

If the column names aren't in the data, remove the first 3 lines of the data step.

If the data is in a file, use infile instead of cards as has been pointed out.

And use the doc and the internet to find out how to modify the example to suit your precise needs.

Good learning! Smiley Surprised)
N/A
Posts: 0

Re: How to read this matrix

The reason I gave general specs in the first post is EXACTLY that I want to learn. I think it is very common to want to undertake a task as I described in the first thread and I am sure I will need to do it over and over again in the future. That is why I didn't give all the specs in the first place because that way I would be asking for a free code that won't work on future needs.
PROC Star
Posts: 1,759

Re: How to read this matrix

Posted in reply to deleted_user
This should get you started.
Please read the doc to understand what it does.
[pre]
data t;
retain COL1-COL3 ; * remember column names;
if _N_=1 then
input (COL1-COL3) ($); * read column names;
else do;
input (ROW VAL1-VAL3) ($); * read row name and values;
put ROW COL1 VAL1; * print out in desired format;
put ROW COL2 VAL2;
put ROW COL3 VAL3;
end;
cards;
y1 y2 y3
x1 z11 z12 z13
x2 z21 z22 z23
run;
N/A
Posts: 0

Re: How to read this matrix

Thanks for the help. The specific .csv files I am trying to convert have hundreds of columns and thousands of rows. I was hoping there is a way to do it other than the "cards".
Regular Contributor
Posts: 165

Re: How to read this matrix

Posted in reply to deleted_user
"Cards" isn't needed but is useful in developing and testing code. For your actual data look into using an infile statement.
Ask a Question
Discussion stats
  • 7 replies
  • 143 views
  • 0 likes
  • 4 in conversation