Reputation: 489
I'd like to the following output, but I couldn't get that. I don't understand how to write raw data of datalines and input options.. Please give me some advice.
data dt00;
input Flavor $14. Quantity : comma.;
datalines;
CHOCOLATE CHIP 10,453
OATMEAL 12,187
PEANUT BUTTER 11,546
SUGAR 12,331
;
proc print data = dt00; run;
* output-----------------------
Flavor Quantity
CHOCOLATE CHIP 10453
OATMEAL 12,187 .
------------------------------;
* I want-----------------------
Flavor Quantity
CHOCOLATE CHIP 10453
OATMEAL 12187
PEANUT BUTTER 11546
SUGAR 12331
------------------------------;
Upvotes: 0
Views: 59
Reputation: 51621
Here are three options.
Place two or more spaces after the variables than can include spaces and use the & input modifier. But you need to make sure there are NOT two adjacent embedded spaces in the middle of the value. Also make sure the use the : modifier on any informats in the INPUT statement.
data dt00;
input Flavor &:$14. Quantity :comma.;
datalines;
CHOCOLATE CHIP 10,453
OATMEAL 12,187
PEANUT BUTTER 11,546
SUGAR 12,331
;
Use a different delimiter. That you need to set on an INFILE statement. Again make sure that any informats listed in the INPUT statement are using the : modifier so input is still list mode.
data dt00;
infile datalines dlm='|';
input Flavor :$14. Quantity :comma.;
datalines;
CHOCOLATE CHIP|10,453
OATMEAL |12,187
PEANUT BUTTER |11,546
SUGAR |12,331
;
Use the DSD option, with whatever delimiter you want, and quote the values that contain the delimiter. Make sure there is only one delimiter character between each value on the line because adjacent delimiter mean there is an empty value between them when using DSD mode.
data dt00;
infile datalines dsd dlm=' ';
input Flavor :$14. Quantity :comma.;
datalines;
"CHOCOLATE CHIP" 10,453
OATMEAL 12,187
"PEANUT BUTTER" 11,546
SUGAR 12,331
;
If you are writing the data from a program then you should use the DSD option and the PUT statement will automatically add the quotes where needed.
data _null_;
file csv dsd ;
set dt00;
put flavor quantity;
format quantity comma.;
run;
Result
CHOCOLATE CHIP,"10,453"
OATMEAL,"12,187"
PEANUT BUTTER,"11,546"
SUGAR,"12,331"
Upvotes: 0
Reputation: 3117
Use another delimiter using the delimiter option in the infile statement, and add the :
modifier to the Flavor column
data dt00;
infile datalines delimiter = "|";
input Flavor :$14. Quantity : comma.;
datalines;
CHOCOLATE CHIP|10,453
OATMEAL|12,187
PEANUT BUTTER|11,546
SUGAR|12,331
;
CHOCOLATE CHIP 10453
OATMEAL 12187
PEANUT BUTTER 11546
SUGAR 12331
Upvotes: 0