BookmarkSubscribeRSS Feed
Garik
Obsidian | Level 7

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? 

6 REPLIES 6
Kurt_Bremser
Super User

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

Astounding
PROC Star

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.

ballardw
Super User

@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.

 

Garik
Obsidian | Level 7

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.

Astounding
PROC Star

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.

ballardw
Super User

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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 1050 views
  • 0 likes
  • 4 in conversation