useR
useR

Reputation: 3082

Character value with embedded blanks with list input

I would like to read following instream datalines

    datalines;
Smith,12,22,46,Green Hornets,AAA
FriedmanLi,23,19,25,High Volts,AAA
Jones,09,17,54,Las Vegas,AA
;

I employed while it read AAA items to team variables but not as div. And how should I place &(ampersand to read character with embedded blanks?)

data scores2;
    infile datalines dlm=",";
    input name : $10. score1-score3 team $20. div $;
    datalines;
Smith,12,22,46,Green Hornets,AAA
FriedmanLi,23,19,25,High Volts,AAA
Jones,09,17,54,Las Vegas,AA
;
run;

Upvotes: 1

Views: 4121

Answers (2)

Joe
Joe

Reputation: 63424

Another way to do this that's often a bit easier to read is to use the informat statement.

data scores2;
    infile datalines dlm=",";
    informat name $10.
             team $20.
             div $4.;
    input name $ score1-score3 team $ div $;
    datalines;
Smith,12,22,46,Green Hornets,AAA
FriedmanLi,23,19,25,High Volts,AAA
Jones,09,17,54,Las Vegas,AA
;
run;

That accomplishes the same thing as using the colon (input name :$10.) but organizes it a bit more cleanly.

And just to be clear, embedded blanks are irrelevant in comma delimited input; '20'x (ie, space) is just another character when it's not the delimiter. What ampersand will do is addressed in this article, and more specifically, if space is the delmiiter it allows you to require two consecutive delimiters to end a field. Example:

data scores2;
    infile datalines dlm=" ";
    informat name $10.
             team $20.
             div $4.;
    input name $ score1-score3 team & $ div $;
    datalines;
Smith 12 22 46 Green Hornets  AAA
FriedmanLi 23 19 25 High Volts  AAA
Jones 09 17 54 Las Vegas  AA
;
run;

Note the double space after all of the team names - that's required by the &. But this is only because delimiter is space (which is default, so if you removed the dlm=' ' it would also be needed.)

Upvotes: 2

in_user
in_user

Reputation: 1958

Notice I have used : before team also ( well you have already used colon operator : for other variables , not sure why did you miss over here) As I have already mentioned in your other query, use : colon operator (tilde, dlm and colon format modifier in list input) which would tell SAS to use the informat supplied but to stop reading the value for this variable when a delimiter is encountered. Here as you had not used this operator , that is why SAS was trying to read 20 chars, even though there was a delimiter in between.

Tested

data scores2;
    infile datalines dlm=",";
    input name : $10. 
          score1-score3 
          team : $20. 
          div : $3.;
datalines;
Smith,12,22,46,Green Hornets,AAA
FriedmanLi,23,19,25,High Volts,AAA
Jones,09,17,54,Las Vegas,AA
;
run;

Upvotes: 2

Related Questions