Statistical programming, matrix languages, and more

Create SAS Dataset from vectors and matrix

Accepted Solution Solved
Reply
SAS Employee
Posts: 3
Accepted Solution

Create SAS Dataset from vectors and matrix

From the helpful post: Writing data from a matrix to a SAS data set - The DO Loop

It shows to make a dataset from vectors with the var statement.

proc iml;

/** create SAS data set from vectors **/

y = {1,0,3,2,0,3}; /** 6 x 1 vector **/

z = {8,7,6,5,6}; /** 5 x 1 vector **/

c = {A A A B B B}; /** 1 x 6 character vector **/

create MyData var {y z c}; /** create data set **/

append; /** write data in vectors **/

close MyData; /** close the data set **/

And from a matrix with the from statement.

proc iml;

/** create SAS data set from a matrix **/

x = {1 2 3,

  4 5 6,

  7 8 9,

  3 2 1 }; /** 4 x 3 matrix **/

create MyData2 from x[colname={"q" "r" "s"}];

append from x;

close MyData2;

Say I have 2 vectors with dimensions, 5x1, and a matrix size 5 x 5.  What is the best way combine or just utilize one of these approaches to get a SAS dataset with 5 records and 7 columns? Thanks!


Accepted Solutions
Solution
‎01-20-2015 01:44 PM
SAS Super FREQ
Posts: 3,416

Re: Create SAS Dataset from vectors and matrix

If they are all the same data type (all numeric or all character), I would use concatenation:
x = {1,2,3,4,5};

y = {5,4,3,2,1};

z = shape(1:25, 5);

m = x || y || z;

create MyData from m;

append from m;

close;

View solution in original post


All Replies
Solution
‎01-20-2015 01:44 PM
SAS Super FREQ
Posts: 3,416

Re: Create SAS Dataset from vectors and matrix

If they are all the same data type (all numeric or all character), I would use concatenation:
x = {1,2,3,4,5};

y = {5,4,3,2,1};

z = shape(1:25, 5);

m = x || y || z;

create MyData from m;

append from m;

close;

SAS Employee
Posts: 3

Re: Create SAS Dataset from vectors and matrix

Thanks Rick!

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 249 views
  • 0 likes
  • 2 in conversation