Help using Base SAS procedures

SAS Error

Accepted Solution Solved
Reply
Contributor
Posts: 50
Accepted Solution

SAS Error

Hi All,

I am using the code -

DATA test;

infile datalines;

input A & $14. B : comma14.;

datalines;

chocolate cream 10,453

tofucutlet 12,187

;

run;

proc print data = test;

run;

I do not know why I get the invalid data error when reading B as the values are in comma format. This occurs when I use ':' in input statement. When I do not use ':', SAS do not read the values of B and also the values of second record for A reads as tofucutlet 12,1.

Could you sugest how to read the values correctly without changing any datalines?

Regards

SK


Accepted Solutions
Solution
‎08-23-2013 03:04 AM
Occasional Contributor
Posts: 9

Re: SAS Error

I think the problem is that SAS is confused because when no delimiter is specified for the infile statement, so the default is a space.

But on your input statement you've specified that for column A, it may contain a space by using the '&'.

So how does SAS know if you mean a space in the value for column A or a space as a delimiter?

If you specify a delimiter (except using a comma as you've also got a comma format being used for B) on the infile statement and also wrap column A's values with double quotes, this will get around this issue.

DATA test;
infile datalines dlm='*' dsd;
input A & $15. B :comma14.;
datalines;
"chocolate cream"*10,453
"tofucutlet"*12,187
;
run;

proc print data = test;

run;

You can use a comma as a delimiter too, only if you wrap the comma formated number in quotes too making sure you use the DSD option.

DATA test;
infile datalines dlm=',' dsd;
input A & $15. B :comma14.;
datalines;
"chocolate cream","10,453"
"tofucutlet","12,187"
;
run;

proc print data = test;

run;

I know I've changed the datalines but I'm not sure if there is another way? Hope this helps as a starting point.

View solution in original post


All Replies
Solution
‎08-23-2013 03:04 AM
Occasional Contributor
Posts: 9

Re: SAS Error

I think the problem is that SAS is confused because when no delimiter is specified for the infile statement, so the default is a space.

But on your input statement you've specified that for column A, it may contain a space by using the '&'.

So how does SAS know if you mean a space in the value for column A or a space as a delimiter?

If you specify a delimiter (except using a comma as you've also got a comma format being used for B) on the infile statement and also wrap column A's values with double quotes, this will get around this issue.

DATA test;
infile datalines dlm='*' dsd;
input A & $15. B :comma14.;
datalines;
"chocolate cream"*10,453
"tofucutlet"*12,187
;
run;

proc print data = test;

run;

You can use a comma as a delimiter too, only if you wrap the comma formated number in quotes too making sure you use the DSD option.

DATA test;
infile datalines dlm=',' dsd;
input A & $15. B :comma14.;
datalines;
"chocolate cream","10,453"
"tofucutlet","12,187"
;
run;

proc print data = test;

run;

I know I've changed the datalines but I'm not sure if there is another way? Hope this helps as a starting point.

Super Contributor
Posts: 307

Re: SAS Error

Another alternative is to use add double spaces between variables and use the & symbol in the input statement to indicate where double spaces are used as delimiters. Here, I've added double spaces at the end of "chocolate cream" and "tofucutlet":

DATA test;

     input A & $14. B : comma14.;

datalines;

chocolate cream  10,453

tofucutlet  12,187

;

run;

Regular Contributor
Posts: 151

Re: SAS Error

Try this code.

length dlm $1;

dlm = ' ';

infile cards dsd dlm=dlm;

input @;

call scan(_infile_,1,start,_n_,dlm);

call scan(_infile_,-1,end,_n_,dlm);

length = end-start-1;

input a
$varying50. length +1 b : comma14.;

keep a b;

cards;

chocolate cream 10,453

tofucutlet 12,187

;

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 222 views
  • 6 likes
  • 4 in conversation