Similar Posts

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

3 Comments

  1. Hello,
    Thank you again!
    Just to notice that this code does not work:
    data Students;
    input name $ 1-18 age 25-27 weight 30-32;
    datalines;
    Joseph 11 32
    Mitchel 13 29
    Sue Ellen 14 27
    ;
    run;
    the start_columns and end_columns are not the good ones and it’s really painful to deal with the name separated by a space. Would you have a simple solution to read this lines ? Thanks.

    1. Hi there!
      You need to adjust the variable lengths and spaces accordingly. For example, the name contains the first and last names, so you need to ensure that the length of 1-10 includes the spaces and all the variable’s values need to be arranged in a column format.

      Column input uses the columns specified in the input statement to determine the length of character variables.

      data Students;
      input name $ 1-10 age 11-13 weight 15-17;
      datalines;
      Joseph     11 32
      Mitchel     13 29
      Sue Ellen 14 27
      ;
      run;
      

      The other alternative is to use the ampersand (&) modifier when you have spaces between values.

      Suppose you use blanks as delimiters and have a character value containing a blank, such as a first and last name. By replacing the colon modifier with an ampersand (&), SAS will continue reading a character value, even if it contains single blanks.

      An important point to note is that when you use the ampersand modifier, follow the variable with two or more spaces.

      data Students;
      input name & $9. age : 2. weight : 2.;
      datalines;
      Joseph 11 32
      Mitchel 13 29
      Sue Ellen 14 27
      ;
      run;
      

      I have used two spaces after “Sue Ellen “.

      1. Thank you Subhro !

        I had built this which works also without adjusting the spaces.

        data Students;
        infile datalines;
        informat name $varying32. age weight 8.;
        input @;
        call scan(_infile_,-2, p, n,' ');
        len=p-2;
        input name $varying32. len age weight;
        drop p n len;
        datalines;
        Joseph 11 32
        Mitchel 13 29
        Sue Ellen 14 27
        Charles Georges Artur 73 30
        ;
        run;
        

        As you can see, we can add any name, whatever its length, it is read correctly.

        A question : do you have a valid email address where we can write to you directly?
        Thanks