12-11-2017 01:44 AM
Sorry I know the Subject isn't very clear but it's the best I can do.
I've seen these numbers from 'x' to 'y', but haven't seen a clear explanation of what they are for?
I got this code from the Step-by-Step Programming with
Base SAS® Software guide.
input #2 Team $ 1-6 #1 Name $ 6-23 IdNumber 1-4
#3 StartWeight 1-3 EndWeight 5-7;
1023 David Shaw
1049 Amelia Serrano
Can someone tell me what do the '1-6', '6-23' etc numbers mean & what function do they perform?
12-11-2017 02:48 AM
input #2 Team $ 1-6 #1 Name $ 6-23 IdNumber 1-4 #3 StartWeight 1-3 EndWeight 5-7;
means that your data is spread on 3 input rows: #1 #2 #3.
I would prefer read rows in proper order: #1 first, then #2 and last #3.
the $ sign means to read a character type data, e.g the NAME is from position 6 to position 23.
IDNUMBER is numeric 4 digits from position 1.
12-11-2017 02:54 AM
these numbers after the variable names in input statement describes -from which position and to which position should this variable read that data from datalines.
in your example
# 2 team $ 1-6 describes that data from second line(#2) in datalines read from position 1 to 6 as team.
and so on..
12-11-2017 03:01 AM
I think I get it but wouldn't this mean you would need to know the minimum & maximum length of the fields in order to write the correct range?
Is this used for alignment of data when presented?
I'm not getting why it's required/used?
12-11-2017 03:17 AM
12-11-2017 03:26 AM
I wasn't aware that I needed a DD to find this out.
So is the purpose of these numbers for SAS to know when to add a new record (Row) using the values after the 'Datalines' statement?
12-11-2017 03:53 AM - edited 12-11-2017 04:00 AM
There are, at least, to types of input data:
1) Fixed length where each variable has its start position and end position the same along all input rows
2) Variable length where except the first one all other may have different stsrt position and different length.
In subh case you need define a chracter to serve as delimiter between the variables.
See option delimiter= (or dlm=) and dsd in the input statement documentation.
In CSV files the comma is rthe delimiter brtween variables.
12-11-2017 02:14 PM
I don't think I'm adding anything new, just saying the same thing using different words.
The INPUT statement is your instructions on how to read lines of incoming data. So 1-6 says take the contents of columns 1 through 6, and make that the value of TEAM. Similarly, take the contents of columns 6 through 23, and make that the value of NAME.
Surely the documentation has examples of this. Perhaps those examples are not complicated by the use of #.
12-11-2017 05:31 PM
Thanks Schmuel, RM6 & Astounding,
What if I ask the question "What happens if I don't use these numbers in my Input statement?
12-11-2017 10:52 PM
well you will not get the desired output you are looking for, if you remove the numbers. In your example you are trying to read name as "David Shaw" , lets say you want to remove the start and the end position then name will read only "David" as there is a space in between the Name variable can not read entire "David Shaw".
if you have trouble finding the end position for the variables, or each time you receive a new data file end position changes
try this, in the below sample, you don't have to define the end position but you will atleast need the start position .
input #2 @1 Team $
#1 @6 Name $30.
#3 @1 StartWeight
@5 EndWeight ;
1023 David Shaw
1049 Amelia Serrano
12-12-2017 01:49 AM
Make sure to post code using either Insert Code or Insert SAS Code buttons in the editor. Otherwise the forum software will try to turn you code into paragraphs. This will mess up the formatting, which is critical if you are trying to read data by position.
If you are looking at your data file using a text editor make sure to use a fixed space font (like COURIER) so that the character positions line up.
If you are using the SAS program editor (under display Manager) you can ask it to insert a ruler line to make it easier to see the positions.
You can also use a data step with the LIST statement to dump some lines to the SAS log. For example this data step will dump the first 10 lines of the file referenced to the log.
data _null_; infile 'mytextfile' obs=10; input; list; run;