BookmarkSubscribeRSS Feed
deleted_user
Not applicable
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!
7 REPLIES 7
ieva
Pyrite | Level 9
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?
deleted_user
Not applicable
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.
ChrisNZ
Tourmaline | Level 20
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! 🐵
deleted_user
Not applicable
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.
ChrisNZ
Tourmaline | Level 20
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;
deleted_user
Not applicable
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".
RickM
Fluorite | Level 6
"Cards" isn't needed but is useful in developing and testing code. For your actual data look into using an infile statement.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 7 replies
  • 876 views
  • 0 likes
  • 4 in conversation