Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Input data into an array

Status
Not open for further replies.

rajTT

Programmer
Mar 2, 2003
10
US
hello, does anyone know how i can input data from specific columns in a data file to an array? Thank you.

example - infile 'c:\data.dat';
input dataset1 17-33;

i need the variable dataset1 to be read into an array?

i hope this makes sense.
 
Will just copy from the doc I gave to my people

Reading into an array
INFILE INFIL; <== FILE ON JCL CARD
INPUT @9 (DESC1-DESC5)($CHAR20.)

THE ABOVE WILL READ IN 5 OCCURANCES OF DESC

-----
INFILE INFIL; <== FILE ON JCL CARD
ARRAY NUMBERS{10}; <== DEFINE SIZE OF ARRAY
INPUT @30 VAR1 $CHAR9.
@319 NUMBERS(*) IB2.0 /* 20 CHARACTERS OF FIXED BIN 15 */
@830 VAR3 $CHAR170. @;

THE ABOVE WILL READ IN 10 OCCURANCES OF NUMBERS INTO THE ARRAY

NOTE CURLY BRACKETS

ALSO NOTE THAT IN ANY SUBSEQUENT DATA STEPS THAT THE 'ARRAY DEFINITION'
NEEDS TO BE REDONE FOR EACH DATA STEP --- I DONT LIKE IT EITHER
-----

DO I = 1 TO DIM(NUMBERS);
IF NUMBERS{I} > 13 THEN NUMBERS{I} = 0;
END;
NOTE NO CURLY BRACKETS ON DIM STATEMENT

----

This help?
 
Hello,
Thank you very much, I will try that. If i don't know the size of the array do i just use the ubound(array) syntax, is that correct? If you don't mind, can you show me an example of that. Thanks again.
 
When you define the array you define it like this
ARRAY JUNK{*} JUNK1 JUNK2 JUNK3;

I have never used an * when reading in - I always define a set space and have an error produced if it gets passed (your mileage may vary)

after that it gets more interesting. You can use the functions LBOUND, HBOUND and DIM

ex. Do I = LBOUND(Junk) to HBOUND(Junk)
or
Do I = 1 to DIM(Junk)

arrays are fun, but like in any other language if you right past what you have defined as the 'space' you can overwrite things and get unexpected errors. Don't forget that you have to define the array in each data step where you plan on using it (and on occasion you will need to RETAIN the whole array - for example I wanted to output 1 record with the info from twelve months - first time through I only got December... I had left off the retain of the array, so SAS zerod it out for each new observation).

Good luck
 
Wow, thank you very much, you've helped me a great deal. Array's definitely are fun and very powerful too, they can make life real easy if coded properly :) Thanks again.
 
one more weird thing. SAS really stores the arrasy as seperate variables (so if I defined an array as WEIGHT{10} I would have WEIGHT1 WEIGHT2 WEIGHT3 WEIGHT4 WEIGHT5 WEIGHT6 WEIGHT7 WEIGHT8 WEIGHT9 WEIGHT10). I always found it a bit annoying that I did not get WEIGHT01 to WEIGHT10 so your deletes and things like that can be coded as WEIGHT1-WEIGHT10

more my own pet peeve than anything
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top