BookmarkSubscribeRSS Feed
Shailesh_R_T
Calcite | Level 5

DATA INPUT_METHODS1;
infile datalines dlm=',' dsd;
INPUT AGE NAME $ HEIGHT WEIGHT;
DATALINES;
    ST 4 30
10 NT 7 40
12 XY 2 42
3 AB 9 37
;
RUN;

6 REPLIES 6
Tom
Super User Tom
Super User

Hard to tell what your code actually is when you post it into the body of your message.

Use the Insert SAS Code icon to get a pop-up window to insert your code.

 

Assuming your code looks like this (Note there is no need for the extra RUN statement after the end of the data step)

DATA INPUT_METHODS1;
infile datalines dlm=',' dsd;
INPUT AGE NAME $ HEIGHT WEIGHT;
DATALINES;
    ST 4 30
10 NT 7 40
12 XY 2 42
3 AB 9 37
;

Because none of the lines have any delimiters and you did not use the TRUNCOVER option you asked SAS to read AGE from the first line, NAME from the second line, etc.  So you should get one observation.

1    DATA INPUT_METHODS1;
2    infile datalines dlm=',' dsd;
3    INPUT AGE NAME $ HEIGHT WEIGHT;
4    DATALINES;

NOTE: Invalid data for AGE in line 5 1-11.
NOTE: Invalid data for HEIGHT in line 7 1-10.
NOTE: Invalid data for WEIGHT in line 8 1-9.
RULE:      ----+----1----+----2----+----3----+----4----+----5----+----6----+--
8          3 AB 9 37
NOTE: Invalid data errors for file CARDS occurred outside the printed range.
NOTE: Increase available buffer lines with the INFILE n= option.
AGE=. NAME=10 NT 7 HEIGHT=. WEIGHT=. _ERROR_=1 _N_=1
NOTE: SAS went to a new line when INPUT statement reached past the end of a
      line.
NOTE: The data set WORK.INPUT_METHODS1 has 1 observations and 4 variables.
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.00 seconds
 Obs    AGE     NAME      HEIGHT    WEIGHT

  1      .     10 NT 7       .         .

 

Did you intend to try using space as the delimiter instead?  That would work for the last three lines.  But since the first line starts with 4 spaces. With the DSD option those adjacent delimiters will indicate missing values.  Let's put a period in for each missing values so you can see what the INPUT will do with that line.

. . . . ST 4 30

So with DLM=' ' that first line would yield missing values for all four variables.  The rest of the line will be ignored.

 Obs    AGE    NAME    HEIGHT    WEIGHT

  1       .               .         .
  2      10     NT        7        40
  3      12     XY        2        42
  4       3     AB        9        37

What is that you want to do?  Do you want to fix the card images so they work with the INPUT code?

Shailesh_R_T
Calcite | Level 5
   ST 4 30
10 NT 7 40
12 XY 2 42
3 AB 9 37
;

now perfect please let it work now

Tom
Super User Tom
Super User

@Shailesh_R_T wrote:
   ST 4 30
10 NT 7 40
12 XY 2 42
3 AB 9 37
;

now perfect please let it work now


That will be hard to read.  It is not delimited because there is no first value on the first line. It is not in fixed columns because the last line has the second field shifted to the left. 

To read that with SAS I would replace the missing values with a period.

DATA INPUT_METHODS1;
  INPUT AGE NAME $ HEIGHT WEIGHT;
DATALINES;
. ST 4 30
10 NT 7 40
12 XY 2 42
3 AB 9 37
;

Result

 Obs    AGE    NAME    HEIGHT    WEIGHT

  1       .     ST        4        30
  2      10     NT        7        40
  3      12     XY        2        42
  4       3     AB        9        37

Note you can use the . to represent missing character variables also.

DATA INPUT_METHODS1;
  INPUT AGE NAME $ HEIGHT WEIGHT;
DATALINES;
. ST 4 30
10 . 7 40
12 XY 2 42
3 AB 9 37
;
 Obs    AGE    NAME    HEIGHT    WEIGHT

  1       .     ST        4        30
  2      10               7        40
  3      12     XY        2        42
  4       3     AB        9        37
Shailesh_R_T
Calcite | Level 5

but clients requirement is not to make correction in data set, instead have to code to make it correct with dsd statement

Tom
Super User Tom
Super User

@Shailesh_R_T wrote:

but clients requirement is not to make correction in data set, instead have to code to make it correct with dsd statement


Ask them to send a version of the file that is actually a delimited file.

 

A delimited file will use quotes around any value that includes the delimiter character. Also any value that has quotes to prevent actual quotes from looking like fields that have delimiter character.

 

If they cannot send a new file and there are only a few places where the formatting of the delimited file is messed up you might be able to make guesses about what they meant.  For example with this file you might guess that the first value is missing when the line starts with a space.

DATA INPUT_METHODS1;
  INPUT @;
  if ' ' ne char(_infile_,1) then input age @;
  INPUT NAME $ HEIGHT WEIGHT;
DATALINES;
  ST 4 30
10 NT 7 40
12 XY 2 42
3 AB 9 37
;

 

Tom
Super User Tom
Super User

To expand on what a delimited file looks like here are some more examples.

If your first line had only one space before the character field value it would work.

DATA INPUT_METHODS1;
  infile datalines dsd dlm=' ' truncover ;
  INPUT AGE NAME $ HEIGHT WEIGHT;
DATALINES;
 ST 4 30
10 NT 7 40
12 XY 2 42
3 AB 9 37
;

But it is better to use a delimiter that does not appear in the data.  Say a pipe character.

DATA INPUT_METHODS1;
  infile datalines dsd dlm='|' truncover ;
  INPUT AGE NAME $ HEIGHT WEIGHT;
DATALINES;
|ST|4|30
10|NT|7|40
12|XY|2|42
3|AB|9|37
;

But really it could be any character.  But if the character appears in the data then that value must be quoted.  So if we decided to use X as the delimiter character then the second field on the third line would need quotes.  Like this:

DATA INPUT_METHODS1;
  infile datalines dsd dlm='X' truncover ;
  INPUT AGE NAME $ HEIGHT WEIGHT;
DATALINES;
XSTX4X30
10XNTX7X40
12X"XY"X2X42
3XABX9X37
;

One thing to check is whether they intended to use TAB character as the delimiter.  Having actual tab characters in a program files is not a good idea.  But having tab characters in in-line datalines of a SAS program is absolutely a bad idea.  If you submit that code from the SAS Display Manager program editor then the tabs will be converted to spaces, ruining the demarcation of the fields in the lines.  Or if another user opens the file their settings might convert the tabs into spaces when the lines are brought into the editor.

 

So if you have a tab delimited text file then always keep the data in that file and do not copy it into the SAS program.

DATA INPUT_METHODS1;
  infile 'Myfile.txt' dsd dlm='09'x truncover ;
  INPUT AGE NAME $ HEIGHT WEIGHT;
RUN;

 

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 6 replies
  • 474 views
  • 0 likes
  • 2 in conversation