2
votes

I am trying to read the following data from a Notepad (text) file into a SAS data set:

name1,124325,08/10/2003,1250.03 
name2,114565,08/11/2003,11115.11 
name3,000007,08/11/2003,12500.02 

When I use this SAS code:

data new;
 filename tfile '~\transact2.txt';  
 infile tfile dsd;  
 input name $ id date mmddyy10. cost 8.2;  
run;

I get this, where cost is all missing:

enter image description here

However, if I just replace dsd with dlm=',', then the cost variable is read in correctly. Why does dsd cause the cost variable to be read in incorrectly?

1

1 Answers

2
votes

dsd does not say "use a delimiter". It tells SAS how to use that delimiter (mostly, saying anything in quotes is treated as one field, and modifying how consecutive delimiters are treated). dlm=',' is necessary to read this in correctly. I'm a bit surprised you got as close to correct as you did. (Fortunately, SAS makes some assumptions here that end up making it work correctly, more-or-less).

Also, you're mixing two styles of input, which isn't allowed.

When you use delimited input, you are using list, not column, input. You can only indicate character/not character, and cannot use informats directly. If you want to embed the informats like you do for the date, you need to use modified column input:

data new;
 filename tfile '~\transact2.txt';  
 infile tfile dsd;  
 input name $ id date :mmddyy10. cost;  
run;

Also note that reading in cost with 8.2 is incorrect. The decimal in an informat is only for reading in 12345678 as 123456.78 (back in the day when you had 80 column cards and didn't want to spend one on the decimal). In general in "modern" SAS you should not be using decimal portion of informat ever. SAS will see the decimal and work it out properly.