BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Hedegaard
Calcite | Level 5

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;

    1 ACCEPTED SOLUTION

    Accepted Solutions
    Haikuo
    Onyx | Level 15

    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

    16 REPLIES 16
    manojinpec
    Obsidian | Level 7

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

    Hedegaard
    Calcite | Level 5

    Hello,

    I have attached the file.

    Haikuo
    Onyx | Level 15

    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

    Hedegaard
    Calcite | Level 5

    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.

    Haikuo
    Onyx | Level 15

    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

    Hedegaard
    Calcite | Level 5

    Hai.kuo, that works perfectly!

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

    Haikuo
    Onyx | Level 15

    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 

    Tal
    Pyrite | Level 9 Tal
    Pyrite | Level 9

    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;

    art297
    Opal | Level 21

    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.


    Tal
    Pyrite | Level 9 Tal
    Pyrite | Level 9

    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?

    art297
    Opal | Level 21

    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?

    Tal
    Pyrite | Level 9 Tal
    Pyrite | Level 9

    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.

    art297
    Opal | Level 21

    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;

    Tal
    Pyrite | Level 9 Tal
    Pyrite | Level 9

    thanks Arthur

    sas-innovate-2024.png

    Don't miss out on SAS Innovate - Register now for the FREE Livestream!

    Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

     

    Register now!

    How to Concatenate Values

    Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

    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
    • 16 replies
    • 5838 views
    • 4 likes
    • 6 in conversation