DATA Step, Macro, Functions and more

read raw data

Reply
Frequent Contributor
Posts: 75

read raw data

[ Edited ]
data cars;
INFILE DATALINES dlm=',';
input Make : $9. Invoice : comma6.;
datalines;
BMW,35,681
Chevrolet,28,197
;
run;

Why I can't read Invoice correctly? What's wrong?

Thank you

PROC Star
Posts: 1,283

Re: read raw data

You are using a comma both as a delimiter and as a digit separator in your data. Not a good idea. You could do something like this

 

data cars;
INFILE DATALINES dlm='|';
input Make : $9. Invoice : comma6.3;
datalines;
BMW|33,890
Chevrolet|17,234
Ford|14,482
Infinity|27,536
;
run;
Super User
Posts: 10,280

Re: read raw data

It contains a delimiter. Delimiters take precedence over commas used as a decimal point.

Use a different delimiter or "mask" the commas with quotes and the dsd option:

data cars;
INFILE DATALINES dlm=',' dsd;
input Make : $9. Invoice : comma6.;
datalines;
BMW,"33,890"
Chevrolet,"17,234"
Ford,"14,482"
Infinity,"27,536"
;
run;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Frequent Contributor
Posts: 75

Re: read raw data

Posted in reply to KurtBremser

You want to say, that i cannot read a raw data as i wrote?

Why i ask, because i saw a lot of palces, where they write it as me, but it doen't work.

for example, https://onlinecourses.science.psu.edu/stat481/node/60/

Super User
Posts: 10,280

Re: read raw data

In that example page, they never use a comma as delimiter.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Frequent Contributor
Posts: 75

Re: read raw data

Posted in reply to KurtBremser

you are right, excuse me

Super User
Super User
Posts: 8,127

Re: read raw data

[ Edited ]

If the field with the embedded delimiter is the last one in the line then just read that field using formatted input instead of list mode input.  Just remove the colon modifier before the format specification in the INPUT statement.

data cars;
  INFILE DATALINES dlm=',' dsd truncover;
  input Make  :$9. Invoice comma6.;
datalines;
BMW,35,681
Chevrolet,28,197
;

If the field is in the middle of the line then you will need to work harder to figure out which parts belong to which fields.

Ask a Question
Discussion stats
  • 6 replies
  • 77 views
  • 0 likes
  • 4 in conversation