turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- General Programming
- /
- Input records of data file into specific array ele...

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

07-22-2015 02:34 PM

I'm sure this must be easy; but I can't figure out how to do it.

I have a data file with several thousand records; 1 record per line. Each record has a variable "i" and a variable "x".

I wish to read the input file, and put "x" into a 1-dimensional array where the array element is identified by "i"; and some values of "i" are missing (e.g., i = 1, 2, 3, 5, 6, 10, ...).

Can anybody help?

Accepted Solutions

Solution

07-22-2015
03:54 PM

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

07-22-2015 03:54 PM

It is already a one dimensional array.

Did you want to convert it into a dataset with one observation and N columns?

Did you want to convert it into a dataset with one variable and N rows?

In either case did you want to insert the missing values for the cells not mentioned in the original data?

Do you know the domain of values for I so that you can set the boundaries for this "array"? Or do you want to dynamically determine them from the data?

Let's assume that you DO know the boundaries you can make a dataset with one row and N columns very easily.

data want ;

do until (eof);

infile 'myfile' end=eof ;

input i x @@ ;

array out x1-x100 ;

if 1 <= i <= dim(out) then out(i)=x;

else put 'Invalid value for I. ' i= x= ;

end;

drop i x ;

run;

All Replies

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

07-22-2015 03:30 PM

Are you asking if you can use an array reference in an INPUT statement?

infile cards missover;

array y[

do while(

input i @;

if missing(i) then leave;

else input y

drop i;

cards;

10 77 1 44 5 44 2 6 3 8

1 99 10 99 2 8

;;;;

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

07-24-2015 01:45 PM

Thank you very much for your help.

It turns out that the suggestion provided by Tom is the most appropriate for my specific situation/application.

Cheers.

Solution

07-22-2015
03:54 PM

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

07-22-2015 03:54 PM

It is already a one dimensional array.

Did you want to convert it into a dataset with one observation and N columns?

Did you want to convert it into a dataset with one variable and N rows?

In either case did you want to insert the missing values for the cells not mentioned in the original data?

Do you know the domain of values for I so that you can set the boundaries for this "array"? Or do you want to dynamically determine them from the data?

Let's assume that you DO know the boundaries you can make a dataset with one row and N columns very easily.

data want ;

do until (eof);

infile 'myfile' end=eof ;

input i x @@ ;

array out x1-x100 ;

if 1 <= i <= dim(out) then out(i)=x;

else put 'Invalid value for I. ' i= x= ;

end;

drop i x ;

run;

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

07-22-2015 09:32 PM

Here's an approach that works when you already have a SAS data set:

proc transpose data=have prefix='X' out=want (drop=_name_);

var x;

id i;

run;

The results can be different than the other proposed solution in a couple of ways. (1) This program will generate an error if you have two observations with the same X value. It's not clear whether that's a good or a bad outcome, but it's an alternative. And (2) This program will only create the variables that are needed. If there is no observation having i=2, for example, it does not create a variable named X2. Again, you have to choose whether that's good or bad.

Good luck.