BookmarkSubscribeRSS Feed
OscarBoots2
Calcite | Level 5

Hi Forum,

 

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.

 

data club2;
input #2 Team $ 1-6 #1 Name $ 6-23 IdNumber 1-4
#3 StartWeight 1-3 EndWeight 5-7;
datalines;
1023 David Shaw
red
189 165
1049 Amelia Serrano
yellow
145 124
;

Can someone tell me what do the '1-6', '6-23' etc numbers mean & what function do they perform?

Thanks

12 REPLIES 12
error_prone
Barite | Level 11
Look at the documentation of the input statement.
OscarBoots2
Calcite | Level 5

I have & then posted this question

Shmuel
Garnet | Level 18

Your code:

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.

 

 

 

RM6
Obsidian | Level 7 RM6
Obsidian | Level 7

hi

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..

 

OscarBoots2
Calcite | Level 5

Thanks RM6,

 

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?

 

Thanks

RM6
Obsidian | Level 7 RM6
Obsidian | Level 7
well usually you should have a data dictionary of some sort which gives you these start position and end position.

as you are trying to read data from multiple lines in to single line of data in to your dataset, it is pretty important to know what data goes to which variable.
OscarBoots2
Calcite | Level 5

Thanks RM6,

 

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?

Shmuel
Garnet | Level 18

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.

Astounding
PROC Star

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 #.

OscarBoots2
Calcite | Level 5

Thanks Schmuel, RM6 & Astounding,

 

What if I ask the question "What happens if I don't use these numbers in my Input statement?

 

Thanks

RM6
Obsidian | Level 7 RM6
Obsidian | Level 7

 

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 .

 

data club2;

input #2 @1 Team $

#1 @6 Name $30.

@1 IdNumber

#3 @1 StartWeight

@5 EndWeight ;

datalines;

 

1023 David Shaw

red

189 165

1049 Amelia Serrano

yellow

145 124

;

run;

 

 

Tom
Super User Tom
Super User

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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 12 replies
  • 3205 views
  • 1 like
  • 6 in conversation