tags:

views:

77

answers:

1
+1  Q: 

Read by Row in SAS

Hi,

How do I read a data file one row at a time in SAS? Say, I have 3 lines of data

1.0 3.0 5.6 7.8
2.3 4.9
3.2 5.3 6.8 7.5 3.9 4.1

I have to read each line in a different variable. I want the data to look like.

A 1.0
A 3.0
A 5.6
A 7.8
B 2.3
B 4.9
C 3.2
C 5.3
C 6.8
C 7.5
C 3.9
C 4.1

I tried a bunch of things. If it has a variable name before every data point, following code works fine

INPUT group $ x @@;

I can't figure out how to go about this. Can someone please guide me on this? Thanks

+4  A: 

i think this will produce almost exactly the result you want. you could apply a format to the Group variable.

data orig;
    infile datalines missover pad;
    format Group 4. Value 4.1;
    Group = _n_;
    do until (Value eq .);
        input value @;
        if value ne . then output;
        else return;
    end;
datalines;
1.0 3.0 5.6 7.8
2.3 4.9
3.2 5.3 6.8 7.5 3.9 4.1
run;

proc print; run;
/*
Obs    Group    Value

  1       1      1.0
  2       1      3.0
  3       1      5.6
  4       1      7.8
  5       2      2.3
  6       2      4.9
  7       3      3.2
  8       3      5.3
  9       3      6.8
 10       3      7.5
 11       3      3.9
 12       3      4.1    */
rkoopmann
Great. Thanks. One more thing, how can I add, say A, B, C instead of 1,2 in the Group. I tried IF _n_=1 THEN SET A; ELSE IF _n_=2 THEN SET B; It does not work. I get some error
yO2gO
Almost: you want if _n_=1 then Group='A'; else if _n_=2 then Group='B'; etc. Or as @rkoopmann suggests, use a format.
sasfrog