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

Dear all,

I can't get SAS to read the following data correctly using list input. Any Ideas???

 

data LFC1819;
input Pos :$2.
Pname &$30.
Fee :comma10.;
datalines;
DM Fabinho 30,000,000
GK Alisson Becker 76,000,000
DM Naby Keita 55,000,000
FW Xherdan Shaqiri 12,000,000
;

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

Actually, it's only the the missing double blank between Pname and Fee that lets your INPUT statement fail. So, another approach would be to let SAS insert the missing blank:

data LFC1819;
input @;
_infile_=prxchange('s/( [\d,\.]+)/ $1/',1,_infile_);
input Pos :$2.
Pname &$30.
Fee :comma10.;
datalines;
DM Fabinho 30,000,000
GK Alisson Becker 76,000,000
DM Naby Keita 55,000,000
FW Xherdan Shaqiri 12,000,000
FW Cristiano Ronaldo (aka CR7) 112,000,000
?? John Doe .
;

I've added two more data lines to demonstrate that digits contained in names (not preceded by a blank, though) and missing values of Fee are handled correctly.

View solution in original post

5 REPLIES 5
BrunoMueller
SAS Super FREQ

I would use a specific delimiter between the data values to avoid any confusion, see code below.

when using the "&" modifier you need two consecutive blanks to delimit the data values. 

 

data LFC1819;
  infile cards dlm=";";
  input
    Pos :$2.
    Pname : $30.
    Fee : comma10.
  ;
datalines4;
DM;Fabinho;30,000,000
GK;Alisson Becker;76,000,000
DM;Naby Keita;55,000,000
FW;Xherdan Shaqiri;12,000,000
;;;;

 

 

Kurt_Bremser
Super User

My preferred action:

go to the source of the data and ask for a better designed layout, with delimiters that do not appear in data fields, or with quotes around data fields, so that the dsd option can be used.

 

Right now, you would have to read the whole input line (_infile_), and extract the first and last "word" for pos and fee (use findc() to find the first and last delimiter), and put the remaining part into pname. You can imagine how "brittle" such a process is, and how much work you'd have maintaining that.

 

A simple delimiter change, and all your problems are gone:

data LFC1819;
infile datalines dlm=';';
input
  Pos :$2.
  Pname :$30.
  Fee :comma10.
;
datalines4;
DM;Fabinho;30,000,000
GK;Alisson Becker;76,000,000
DM;Naby Keita;55,000,000
FW;Xherdan Shaqiri;12,000,000
;;;;
run;
s_lassen
Meteorite | Level 14

As suggested by @BrunoMueller, it would be easier if you rewrote your infile. But assuming that you get it from someone else, that is not always possible. In which case you will have to read the data to temporary fields and then parse, e.g.:

data LFC1819;
length pos $2 Pname $30 str2 str3 $20;
infile cards truncover;
input pos--str3;
if lengthn(str3) then do;
  call catx(' ',pname,str2);
  Fee=input(str3,comma10.);
  end;
else
  Fee=input(str2,comma10.);
drop str2 str3;
datalines;
DM Fabinho 30,000,000
GK Alisson Becker 76,000,000
DM Naby Keita 55,000,000
FW Xherdan Shaqiri 12,000,000
;run;
FreelanceReinh
Jade | Level 19

Actually, it's only the the missing double blank between Pname and Fee that lets your INPUT statement fail. So, another approach would be to let SAS insert the missing blank:

data LFC1819;
input @;
_infile_=prxchange('s/( [\d,\.]+)/ $1/',1,_infile_);
input Pos :$2.
Pname &$30.
Fee :comma10.;
datalines;
DM Fabinho 30,000,000
GK Alisson Becker 76,000,000
DM Naby Keita 55,000,000
FW Xherdan Shaqiri 12,000,000
FW Cristiano Ronaldo (aka CR7) 112,000,000
?? John Doe .
;

I've added two more data lines to demonstrate that digits contained in names (not preceded by a blank, though) and missing values of Fee are handled correctly.

Dennis_K
Obsidian | Level 7

That solve the mystery!! Smiley LOL

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!

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
  • 5 replies
  • 758 views
  • 3 likes
  • 5 in conversation