DATA Step, Macro, Functions and more

Array

Reply
Contributor
Posts: 33

Array

Hello there. I have a question on arrays. Hope find the answer here.

if I should load 2 dimensional array 1st one is " set mydataset" (variables  are:  year $ day ) and the other one is under "datalines" variables are  through 1to 5.

 

So if my code is

 If _n_ =1 then

   Do year = 1 to 5;

      Do job = 1 to 5;

          Input myarray {year, job} @;

     End;

End;

 

 

The "year" variable  is being taken from the set "mydataset" and the "job" variable from the datalines? Is it right? 

Super User
Posts: 6,928

Re: Array

input is the statement for reading from external (text) files. SAS datasets are read by the set statement.

SAS arrays are constructs that allow you indexed access to data step variables.

 

For further help, supply:

- example data for the datasets (data step with datalines). If that is too hard for you at the moment, at least supply structure information (variables)

- what result you want to achieve

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Posts: 5,079

Re: Array

Both YEAR and JOB are created by the DO loops.

 

This program takes 25 values from DATALINES and loads them into the array.  Within DATALINES, the values might be all on one line, or they might be spread across as many as 25 lines.

Super User
Posts: 10,483

Re: Array

[ Edited ]

Garik wrote:

Hello there. I have a question on arrays. Hope find the answer here.

if I should load 2 dimensional array 1st one is " set mydataset" (variables  are:  year $ day ) and the other one is under "datalines" variables are  through 1to 5.

 

So if my code is

 If _n_ =1 then

   Do year = 1 to 5;

      Do job = 1 to 5;

          Input myarray {year, job} @;

     End;

End;

 

 

The "year" variable  is being taken from the set "mydataset" and the "job" variable from the datalines? Is it right? 


Who knows what may be going on with "mydataset", you don't show it in the code. Behavior would be quite different depending on whether the Set statement, which is how to reference an existing dataset, is before or after the shown code. If you have datalines then the way the datalines are shown along with any options for Infile Datalines become very important.

 

Show the entire datastep.

 

And describe what you are attempting to do.

 

Contributor
Posts: 33

Re: Array

the code writtren for "look up". the main question is: if there are two data sets one in the datalines form and the other one as an external file, could sas read variables from both of sources?

to merge them, in a nutshell.

 

thanks in advance.

Super User
Posts: 5,079

Re: Array

SAS could read from both in the same DATA step, and could combine the data sources.  "MERGE" means something specific in SAS, however, and would not apply to data read from DATALINES.

 

From your description, after loading the array, the SET statement will read a data set that contains MONTH and JOB.  Those values will be used to look up the matching element from the array.

 

You haven't really shown that part of the program, so this is just an educated guess based on your description.

Super User
Posts: 10,483

Re: Array

Show examples of the data set, the datalines and desired result.

Here's a couple examples that you can run to see different results.

data work.one;
   input x;
datalines;
1
2
3
;
run;


data work.test1;
   set work.one;
   array a{3} $ 10;
   do i = 1 to 3;
   input a[i] $ @;
   end;
datalines;
one
two 
three
four
five 
six
seven
eight
nine
;
run;
data work.test2;
   array a{3} $ 10;
   do i = 1 to 3;
   input a[i] $ @;
   end;
   set work.one;
datalines;
one
two 
three
four
five 
six
seven
eight
nine
;
run;
data work.test3;
   array a{3} $ 10;
   do i = 1 to 3;
   set work.one;

   input a[i] $ @;
   end;
datalines;
one
two 
three
four
five 
six
seven
eight
nine
;
run;

Note the the order of statements can mean minor to very major different outputs

 

In general I would be very tempted to say to execute the datalines portion to create a separate data step and then combine the data.

There are a very large number of ways to combine two existing SAS data sets.

Ask a Question
Discussion stats
  • 6 replies
  • 385 views
  • 0 likes
  • 4 in conversation