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

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?

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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;

View solution in original post

4 REPLIES 4
data_null__
Jade | Level 19

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

data ar;
   infile cards missover;
  
array y[10];
   do while(1);
      input i @;
      if missing(i) then leave;
      else input y @;
      end;
  
drop i;
   cards;
10 77 1 44 5 44 2 6 3 8
1 99 10 99 2 8
;;;;
   run;

david_fredericton
Calcite | Level 5

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.

Tom
Super User Tom
Super User

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;

Astounding
PROC Star

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.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 846 views
  • 0 likes
  • 4 in conversation