bZhang
bZhang

Reputation: 307

How to read data in sas one row at a time

say I have two rows of data I try to read in.

 cody: 10 9 20 18
 john: 4 5 1 2

and I want to read them in a two row style in datalines, like such:

 input cody john @@;
     datalines;
     10 9 20 18
     4 5 1 2
 run;

But this reads it in like cody: 10 20 4 1 john: 9 18 5 2

How do I fix this?

Upvotes: 0

Views: 457

Answers (2)

Joe
Joe

Reputation: 63434

You'd need to read in the CODY lines all at once, then the JOHN lines all at once. It's unclear what the final data structure should look like, but this is one possibility, and then you can restructure this how you wish, perhaps with PROC TRANSPOSE.

Basically, I assign name to the proper name (using an array here, but you can do this in better ways, data-driven ways, depending on your data). Then I loop and tell SAS to keep reading in data until it is unable to read any more, using the truncover option (or missover is also fine) to make sure it doesn't skip to the next line, and output a new row for each value.

data want;
  array names[2] $ _temporary_ ("Cody","John") ;
  infile datalines truncover;
  do _name = 1 to 2;
    name = names[_name];
    do _i = 1 by 1 until (missing(value));
      input value @;
      if not missing(value) then output;
    end;
    input;
  end; 
  drop _:;
  datalines;
10 9 20 18
4 5 1 2
run;

Upvotes: 2

Matteo Felici
Matteo Felici

Reputation: 1107

I think that the solution to your problem is to use the names as another column, not as variables, like this:

data foo;
    input var1 $ var2 var3 var4 var5;
    datalines;
cody 10 9 20 18
john 4 5 1 2
;
run;

Upvotes: 1

Related Questions