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

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

1 ACCEPTED SOLUTION

Accepted Solutions
PaulLee
Fluorite | Level 6

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

3 REPLIES 3
PaulLee
Fluorite | Level 6

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.

Fugue
Quartz | Level 8

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;

Oleg_L
Obsidian | Level 7

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

;

sas-innovate-2024.png

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.

 

Register now!

What is Bayesian Analysis?

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.

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
  • 3 replies
  • 1013 views
  • 6 likes
  • 4 in conversation