DATA Step, Macro, Functions and more

Reading data from text-file into array

Accepted Solution Solved
Reply
Contributor
Posts: 24
Accepted Solution

Reading data from text-file into array

Hi,

I have a text file containing 20 x 20 numbers, which are all of two digits. I am trying to read these numbers into a 20 x 20 array (which I call "a"). When I put the array a in my code below, I don't get a 20 x 20 array, but something enormous, which looks more like each entry a[i,j] of the array contains 20 entries by itself. Can anyone see where I went wrong?

Kind regards,

Hedegaard.

proc import datafile = "C:\Documents and Settings\Desktop\grid.txt" out=work.griddata replace;

     delimiter = " ";
     getnames = no;
     guessingrows = 20;
run;

data grid;
     set griddata;

array var{1:20} VAR1-VAR20;
array a{20,20};

do i = 1 to 20;
     a[i,_N_] = var;
end;

put a

  • ;
  • run;


    Accepted Solutions
    Solution
    ‎04-04-2012 08:52 AM
    Respected Advisor
    Posts: 3,156

    Re: Reading data from text-file into array

    Posted in reply to Hedegaard

    Hm, in that case, you could try the following, it may meet your need?

    data grid;

    infile "C:\Documents and Settings\Desktop\grid.txt";

    array a{20,20};

    do i = 1 to 20;

       do j=1 to 20;

         input a[i,j] @@;

    end;

    end;

    put a

  • ;
  • run;

    Haikuo

    View solution in original post


    All Replies
    Frequent Contributor
    Posts: 139

    Reading data from text-file into array

    Posted in reply to Hedegaard

    Can please send the dummy text file which you are trying to read?

    Contributor
    Posts: 24

    Re: Reading data from text-file into array

    Posted in reply to Hedegaard

    Hello,

    I have attached the file.

    Attachment
    Respected Advisor
    Posts: 3,156

    Re: Reading data from text-file into array

    Posted in reply to Hedegaard

    two-dimention arrays is denoted as array(row, column). So for your purpose, it will be easier to read if you define your array:

    a[_n_,i] = var;

    instead of a[i,_N_] = var;

    Other than that, I don't see any problems, it works the way it should. Both will give you 400 new variables, so I don't understand your question. You probably was looking for something proc iml does, but not arrays.

    Regards,

    Haikuo

    Contributor
    Posts: 24

    Re: Reading data from text-file into array

    Hm, I think I understand now.

    What bothered me was that there seemed to be a 20 rows of each entry of the array a, so that when I asked SAS to "put a[i,j];" for some specific choice of i and j, then I'd see a 20 entry long list in the log, where all but one entry was missing. This might be because it creates the array for each of the twenty rows in the input file?

    I am interested in working with the final and filled up array, so I suppose I could encapsule whatever I want to do with the array in an "if _N_=20" condition.

    Edit: And it would appear I also need to add a "retain" statement.

    Solution
    ‎04-04-2012 08:52 AM
    Respected Advisor
    Posts: 3,156

    Re: Reading data from text-file into array

    Posted in reply to Hedegaard

    Hm, in that case, you could try the following, it may meet your need?

    data grid;

    infile "C:\Documents and Settings\Desktop\grid.txt";

    array a{20,20};

    do i = 1 to 20;

       do j=1 to 20;

         input a[i,j] @@;

    end;

    end;

    put a

  • ;
  • run;

    Haikuo

    Contributor
    Posts: 24

    Re: Reading data from text-file into array

    Hai.kuo, that works perfectly!

    I'm not sure why though? What does the input command do exactly?

    Respected Advisor
    Posts: 3,156

    Re: Reading data from text-file into array

    Posted in reply to Hedegaard

    Cause you need 400 variables in ONE line populated with values. If you already have them in a dataset of 20 lines (rows, obs), it will be a lot harder to achieve your goal. 'input @@' will let you keep reading until the end of the file, which can be considered as just one line if numer of your variables are equal or more than the number of the data elements in your raw flat file.

    Haikuo 

    Super Contributor
    Super Contributor
    Posts: 444

    Re: Reading data from text-file into array

    hey Haikuo,

    related to this problem...

    when i use the code below everything is fine except that in the log i get 400 observations,one for each element of the array

    data one;

    infile ' file';

    input a1-a400;

    array a(20,20) a1-a400;

    do i=1 to 20;

    do j=1 to 20;

    put a(i,j);

      end;end;

      drop i j;

      proc print;

      run;

    then when i try this code below i get

    ERROR: Array subscript out of range at line 871 column 5.

    do you know why? :smileysilly:

    data one;

    infile ' file';

    input a1-a400;

    array a(20,20) a1-a400;

    do i=1 to 20;

    do j=1 to 20;

      end;end;

    put a(i,j);

      drop i j;

      proc print;

      run;

    PROC Star
    Posts: 7,471

    Re: Reading data from text-file into array

    Your put statement is after your two loops, thus i=21 and j=21.  Therefore, when the system confronts:

    put a(i,j);


    It is being asked for an array value that is outside of the parameters you declared.


    Super Contributor
    Super Contributor
    Posts: 444

    Re: Reading data from text-file into array

    Thanks Arthur.

    After changing the 20 to 19 SAS did not give me any errors but when i have put out of the loop SAS does not write the observations onto the log window.Why?

    PROC Star
    Posts: 7,471

    Re: Reading data from text-file into array

    Not sure what you did or why.  With a 20 by 20 matrix you have 400 values.  By having a put statement within the loop it is being addressed 400 times thus outputting 400 values.

    When you move it outside of the loop it is only outputting 1 value.  Why did you move it outside of the loop in the first place?

    Super Contributor
    Super Contributor
    Posts: 444

    Re: Reading data from text-file into array

    thanks Arthur.

    With PUT inside the loop,SAS is writing 400x1 observations in the log and i kinda wanted to see something like a matrix form of results so i thought if i play with PUT would eventually get that result lol but i guess when it is out SAS only writes the last element of the array.

    PROC Star
    Posts: 7,471

    Re: Reading data from text-file into array

    How about?:

    data grid;

      infile "C:\Documents and Settings\Desktop\grid.txt";

      array a{20,20};

      do i = 1 to 20;

        do j=1 to 20;

          input a[i,j] @@;

          if j lt 20 then put @(j*4) a[i,j] @;

          else put a[i,j];

        end;

      end;

    run;

    Super Contributor
    Super Contributor
    Posts: 444

    Re: Reading data from text-file into array

    thanks Arthur

    🔒 This topic is solved and locked.

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

    Discussion stats
    • 16 replies
    • 1799 views
    • 4 likes
    • 6 in conversation