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
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.
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.
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;
Try this code.
data test;
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
;
Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.
Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.