02-07-2016 03:55 PM - edited 02-07-2016 03:56 PM
I'm preparing for the Base SAS Programming certification and am currently reading chapter 17: reading free-format data (Third Edition). I tried executing the code snippet on page 543, in which the data step reads a range of numeric variables and specifies a format for it. I am working on SAS University Edition.
The following is my code snippet:
filename sf '/folders/myfolders/salesfmtd.txt'; data salesfmtd; infile sf; input Name $ (Sales1-Sales3) (7.); run;
The salesfmtd.txt file has the following contents:
X 1500 1280 1800
Y 1260 1700.345 1900
Z 1600.076 1450 1720
The output data contains just one record as follows:
X . . .
i.e. the values are missing and represented by a period.
The log displays the following notes:
02-07-2016 05:31 PM
Your range of variables are not character or formated. So you do not nedd to enclose them in parentheses.
This will work
input Name $ Sales1-Sales3 7.;
02-07-2016 11:13 PM
Thanks for your reply, Zaki. I modified the code according to what you said, but I still didn't get the desired output.
Following are the log messages:
02-08-2016 12:18 AM
02-08-2016 02:00 PM - edited 02-08-2016 04:06 PM
As you're preparing for the certification, you should have come across the terms "formatted input" and "list input." Your raw data file seems to be space-delimited and the column widths vary between observations. This is a typical situation calling for list input. You can specify informats in the (list) INPUT statement (or, alternatively, in an INFORMAT statement) -- this is then called modified list input --, but the syntax for this requires the informat names to be prefixed with colons:
input Name $ (Sales1-Sales3) (:7.);
The above INPUT statement should work well with your data. All four variables are read using list input. Without the colon, Sales1 - Sales3 would be read using formatted input, which has implications on
(For details please see the documentation or, for another user's example, this older thread.)
As Reeza pointed out, there is actually no need to specify informat 7. here (the values are read correctly by default). Also, the width specification of an informat used in modified list input (not: formatted input) is ignored anyway in the reading process.* So, in your example you could even specify informat 1. (which looks as if it was way too short for your numeric raw data values) and nevertheless all the digits would be read correctly with modified list input.
* (But it does have an impact on the length of character variables if their length has not been set previously!)