BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
animesh123
Obsidian | Level 7

data abc;
input stud $35. Age;
cards;
animesh mardi 25
nahir sharma 26
agent rank 22
niharika ghatgey 23
pushpanjali kujur 22
;
run;

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

You told it to read the first 35 bytes into the first variable.

You can use the LIST statement

data _null_;
  input ; 
  list;
cards;
animesh mardi 25
nahir sharma 26
agent rank 22
niharika ghatgey 23
pushpanjali kujur 22
;

to see a nice listing that shows why that cannot work for the data you posted.

RULE:      ----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0
675        animesh mardi 25
676        nahir sharma 26
677        agent rank 22
678        niharika ghatgey 23
679        pushpanjali kujur 22
NOTE: DATA statement used (Total process time):

If you want your code to work the data need to look like this:

*---+----1----+----2----+----3----+----4-;
cards;
animesh mardi                      25
nahir sharma                       26
agent rank                         22
niharika ghatgey                   23
pushpanjali kujur                  22
;

You could add the : modifier to the input statement so that it only uses the informat to GUESS what length you wanted to define the variable.  But you would still need to also add the & modifier and change the data to have two spaces after the string (and not have two spaces embedded in the string).

data abc;
  input stud & :$35. Age;
cards;
animesh mardi  25
nahir sharma  26
agent rank  22
niharika ghatgey  23
pushpanjali kujur  22
;

You could also change the data to use something other than space are the delimiter and add an INFILE statement to tell SAS to use that delimiter.

data abc;
  infile cards dlm='|';
  input stud :$35. Age;
cards;
animesh mardi|25
nahir sharma|26
agent rank|22
niharika ghatgey|23
pushpanjali kujur|22
;

View solution in original post

3 REPLIES 3
Kurt_Bremser
Super User

When you use formatted INPUT, you have to provide enough characters in the data before the next column.

All stud entries must be 35 characters long (pad with blanks).

Or you use an INFILE statement with the DSD option, quotes around the stud values, and the colon modifier for the $35. informat.

data abc;
infile datalines dsd;
input stud :$35. Age;
cards;
"animesh mardi" 25
;

 

ballardw
Super User

One thing you need to be aware of with this forum. The forum software will reformat text pasted into the main message window. So it is possible that the data step you pasted is not what we actually see. You should post code, especially data step code reading inline data, into a text box opened on the forum using the </> icon that appears above the main message window.

 

It will then appear set off as in @Kurt_Bremser 's post.

Tom
Super User Tom
Super User

You told it to read the first 35 bytes into the first variable.

You can use the LIST statement

data _null_;
  input ; 
  list;
cards;
animesh mardi 25
nahir sharma 26
agent rank 22
niharika ghatgey 23
pushpanjali kujur 22
;

to see a nice listing that shows why that cannot work for the data you posted.

RULE:      ----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0
675        animesh mardi 25
676        nahir sharma 26
677        agent rank 22
678        niharika ghatgey 23
679        pushpanjali kujur 22
NOTE: DATA statement used (Total process time):

If you want your code to work the data need to look like this:

*---+----1----+----2----+----3----+----4-;
cards;
animesh mardi                      25
nahir sharma                       26
agent rank                         22
niharika ghatgey                   23
pushpanjali kujur                  22
;

You could add the : modifier to the input statement so that it only uses the informat to GUESS what length you wanted to define the variable.  But you would still need to also add the & modifier and change the data to have two spaces after the string (and not have two spaces embedded in the string).

data abc;
  input stud & :$35. Age;
cards;
animesh mardi  25
nahir sharma  26
agent rank  22
niharika ghatgey  23
pushpanjali kujur  22
;

You could also change the data to use something other than space are the delimiter and add an INFILE statement to tell SAS to use that delimiter.

data abc;
  infile cards dlm='|';
  input stud :$35. Age;
cards;
animesh mardi|25
nahir sharma|26
agent rank|22
niharika ghatgey|23
pushpanjali kujur|22
;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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
  • 3 replies
  • 421 views
  • 0 likes
  • 4 in conversation