Taking Substrings, unpacking" a string - SAS Programming

In this example, you have a string which represents 10 single-digit scores for a person.This "packing" of data is done to either save space or to reduce the number of variables per person.In many database packages (especially those for PC's), there is a limit to the number of variables that may be contained in a single database.By using a single variable to represent multiple scores, you can "trick"the program into functionally holding more variables than the actual maximum.

After you transfer this data set into a SAS System file, you have a 10-byte character variable from which you want to extract the 10 scores.The SUBSTR function, placed in a loop with the index "pointing"to each of the scores in turn, accomplishes your goal.Here it is: (Data set OLD contains a character variable STRING which holds the 10 scores.)

Example

DATA SCORE;
SET OLD;
ARRAY XJ10J X1-X10;
DO J-l TO 10?
x[j]*iNPur ISUBSTR {STRING,j,i),i,);
END;
DROP STRING J;
RON;

Here you place the SUBSTR function inside a DO loop which gets executed 10 times, once for each value of the counter J (1 to 10). Each time the loop is executed, another byte is extracted from STRING.Each of the X variables is equal to the value of one of the bytes in the 10-byte variable STRING. Since you want the variables X1 to X10 to be numeric, you create the array that holds them as a numeric array.

Since the original variable STRING is character, you use the INPUT function to make the characterto- numeric conversion (see Example 5).If you omit the INPUT function, the SAS System performs an automatic character-to-numeric conversion.We prefer programming the conversion ourselves.


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

SAS Programming Topics