Reading Parts of Your Data More Than Once FEATURES: @ point control - SAS Programming

There are often times when it is useful to read the same raw data columns more than once to create different SAS data set variables.You can read the exact same range of columns multiple times and create variables of different types (e.g.numeric, character, date, etc.),or read a subset of a previously read column range into a new variable,or even read overlapping ranges into different variables.

Suppose you are dealing with inventory data where it is important to know in which state a part was manufactured,the weight of the part,and the year in which it was made.Each part has a 14-character part ID made up of four components: a two-character state code,a three-digit part number,a three-digit part weight,and a six-digit manufacture date in MMDDYY format.Each part also has a description and a quantity-on-hand value.

You have to build a SAS data set that includes the following independent variables:PARTID (part ID),ST (state of manufacture),WT(part weight),YR(year of manufacture),PARTDESC (part description),and QUANT (quantity on hand.)PARTID must be read as a character variable because it contains alphanumeric characters (the state abbreviations).You also want ST read separately.

WT must be read as a numeric variable because you have to do arithmeticcalculations with it(e.g.total weight for all pieces in the warehouse).You only need the year at present for YR,so you don't read the entire date.Once again,there are many ways to accomplish your task.The following coderepresents one method:

Example

DATA PARTS;
INPUT @1 PARTID $14,
81 ST $2.
@6 WT 3.
813 YR 2.
§16 PARTDESC $24.
@41 QUANT 4.;
DATALINES;
NY101110060172 LEFT-HANDED WHIZZER 233
MA102085112885 FULL-NOSE BLINK TRAP 1423
CA112216111291 DOUBLE TONE SAND BIT 45
NC222845071970 REVERSE SPIRAL RIPSHANK 876
;
PROC PRINT DATA=PARTS;
TITLE 'Example ';
RUN;

PARTID is first read as a 14-byte character variable starting in column 1. By re-reading columns 1 and 2, you obtain a value for the 2-byte character variable, ST.You then read data from within the same column range that you read as character for PARTID and obtain two numeric variables: WT fromcolumns 6-8 and YR from columns 13-14.

The INPUT statement completes the process by reading in a 24-byte character variable PARTDESC starting in column 16 and a 4-byte numeric variable QUANT starting in column 41.The previous code yields the following output:

Output from Example- Reading Parts of Your Data More Than Once

Output from Example- Reading Parts of Your Data More Than Once

There are actually other ways to accomplish these goals (but there aren't better ways to get basic concepts across.) You could use character substring functions and date functions and PUT and INPUT functions, etc.,but let's wait until Chapter 5, "SAS Functions," to meet these powerful tools.


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

SAS Programming Topics