BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
margi123
Fluorite | Level 6

----|----10---|----20---|----30
24613 $25.31
The following SAS program is submitted:

data inventory; 
    infile 'product';
    input  idnum 5. @10 price;
run;

 

In this sinario, i am trying to ru  data in SAS univercity adition but i dont know how do i import this PRODUCT raw data file in sas code.

And other thing is that i think the answer would come 'missing value' but in the one site i saw the answer is 25.31!!

So i really dont understand this logic of these type of raw data examples.Can you please help me out?

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

We can't tell since the way you pasted the data line this website might have removed some of the blank space between the words in the lines.

The right answer depends on the number of spaces between the '3' and then '$' since that will determine what the input statement sees when it tries to read a value into price starting from the tenth column.

data inventory; 
*   infile 'product';
    input  idnum 5. @10 price;
*---|----10---|----20---|----30;
cards;
24613 $25.31 One space
24613  $25.31 Two spaces
24613   $25.31 Three spaces
24613    $25.31 Four spaces
;;;;

With one space you will get .31, with two you will get 5.31 and with three you will get 25.31. Any more and the '$' will make it generate an error (but NOT a SYNTAX error) and set the value to missing.

So of the four answers on the test only A and C are possible right answer.  Which one is right depends on whether there are 3 spaces or more.

View solution in original post

6 REPLIES 6
collinelliot
Barite | Level 11

You need to apply the proper informat (dollar8.2 ,for example) for the price variable because the "$" is text and you're trying to import it as a numeric variable.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Sorry, your post is very unclear.  This code:

data inventory;
  input  idnum 5. @10 price;
datalines;
24613 $25.31
;
run;

Same as yours will yield:

idnum     price

24613     0.31

The reason being is that the @ is moving the pointer along the line 10 characters. So it reads in idnum, 5 characters from the length given.  Then it moves the file pointer from character 1 (which is where it read the last item from) across 10 characters, which if you count it is the . so it reads in .31 for the second variable.  

 

margi123
Fluorite | Level 6

Sir,

But here the options are like A)25.31

                                             B)$25.31

                                             C).(missing numric value)

                                             D)Syntax error

Then what will be the answer i select? We know the correct answer is .31!

Tom
Super User Tom
Super User

We can't tell since the way you pasted the data line this website might have removed some of the blank space between the words in the lines.

The right answer depends on the number of spaces between the '3' and then '$' since that will determine what the input statement sees when it tries to read a value into price starting from the tenth column.

data inventory; 
*   infile 'product';
    input  idnum 5. @10 price;
*---|----10---|----20---|----30;
cards;
24613 $25.31 One space
24613  $25.31 Two spaces
24613   $25.31 Three spaces
24613    $25.31 Four spaces
;;;;

With one space you will get .31, with two you will get 5.31 and with three you will get 25.31. Any more and the '$' will make it generate an error (but NOT a SYNTAX error) and set the value to missing.

So of the four answers on the test only A and C are possible right answer.  Which one is right depends on whether there are 3 spaces or more.

margi123
Fluorite | Level 6

Yeah excatlly how many spaces between them its not show properly. Thats why i am confused too. But thanks, i am satisfied with your answer!

ballardw
Super User

Understanding reading data files is a combination to two major elements. One is the data source and INFILE statement tells SAS things about that, how long a record may be, delimiters, data location . The second is the INPUT statement and it's options which say how to read each record. There are 4 main ways to read data: List, column, named and formatted.

 

LIst input statements look like this:

INPUT var1 var2 var3;

This type of data relies on something in the data that lets SAS know where one value starts and ends. Typically this would be space though other characters could be used and are referred to as Delimiters common ones being comma and pipe ( the | character).

 

Column input has something to indicate which column a variable starts at or specific start and end columns.

INPUT var 10-25; Says to read the value of Var starting at column10 and ending at 25.

Column input involves a "pointer", this is a logical construct the program maintains that keeps track of where the data is being read.

You can move that pointer to a specific location on a line with @ followed by a numeric value. @10 says to move the pointer to the 10th column. You can also make relative adjustments by using plus or minus followed by a number: +5 moves the pointer 5 characters to the right from the current position  +(-4) would move the point left 4 characters. You can also specify to move the pointer to the position after a string value @'Fred' would start reading immediately after where Fred appears in the data on the current line.

 

Formatted input specifies a format to read on the input statement and applies the format to the values at the location of the input pointer.

 

Named input is used for data sources that look like: Cost=123 Sales=456

Named input statement would look like : Input cost= sales= ; SAS searchs for Cost= and reads the value then looks for Sales= and reads that value.

 

These styles can be combined.

So your input statement:

input  idnum 5. @10 price;

starts with LIST as there is no position indicated, so the data line is searched for the first non-blank character and attempts to read the value using FORMATTED input with the F5 format, in effect limiting the data to a 5 digit integer value. the @10 then advances the column pointer to position 10

If you posted code using the code box with the {i} icon, a very strongly recommended practice it would probably look like this

----|----10---|----20---|----30
  24613 $25.31

You posted example doesn't do that because the main window of the forum uses a proportional font and will strip some formatting characters when posted (hence the recommendation to post code with the {i} code box which maintains formatting and uses a monospace font so you can count characters.

 

The line with the dashes is used so that you can tell which column data is in. Every 10th position the number is shown.

In my code box above you can see that the numeral 2 is below the 10. So the @10 moved the column pointer to read at the position of the 2. And that is where the pointer is when the variable Price is supposed to be input. So it reads the value as 25.31 as price is a numeric value (since it wasn't indicated to be character that is the SAS defaut) and reads until it finds a space, end of line or finds a non-numeric value (25.31x ) which would usually be an error as the x isn't numeric.

 

The program does not expect you to actuall read from a file but infile 'path/file.txt'; would tell SAS where to find the input file.

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
  • 6 replies
  • 2963 views
  • 4 likes
  • 5 in conversation