Hi,
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:
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.;
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:
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!)
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.