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

I am a beginning user and have a problem with creating a two-dimensional array. Any advice would be highly appreciated. Thank you in advance. 

 

I have a data set with 6 variables (ID and var1-var5) and 10 people. The data set is in Excel. I want to create a two-dimensional array (people by var), so I need a 10x5 array.

 

I have no problem importing the data into SAS. It correctly displays the data table:

 

table.jpg

 

But when I try to ask for a 10x5 array, this is what I get (goes on to ex50): 

 

table2.png

 

 

Here is my syntax: 

 

PROC IMPORT OUT = WORK.Example DATAFILE = "C:\Desktop\Example.xls"
DBMS = xls REPLACE;
SHEET = "Example";
GETNAMES = YES;
RUN;


proc print data = example;
run;

 

data example;
array ex{10,2:6};
run;

 

proc print;
run;

 

 

Here is the log: 

 

1632 PROC IMPORT OUT = WORK.Example DATAFILE = "C:\Desktop\Example.xls"
1633 DBMS = xls REPLACE;
1634 SHEET = "Example";
1635 GETNAMES = YES;
1636 RUN;

NOTE: The import data set has 10 observations and 6 variables.
NOTE: WORK.EXAMPLE data set was successfully created.
NOTE: PROCEDURE IMPORT used (Total process time):
real time 0.01 seconds
cpu time 0.01 seconds


1637 proc print data = example;
1638 run;

NOTE: There were 10 observations read from the data set WORK.EXAMPLE.
NOTE: PROCEDURE PRINT used (Total process time):
real time 0.07 seconds
cpu time 0.00 seconds


1639 data example;
1640 array ex{10,2:6};
1641 run;

NOTE: The data set WORK.EXAMPLE has 1 observations and 50 variables.
NOTE: DATA statement used (Total process time):
real time 0.01 seconds
cpu time 0.01 seconds


1642 proc print;
1643 run;

NOTE: There were 1 observations read from the data set WORK.EXAMPLE.
NOTE: PROCEDURE PRINT used (Total process time):
real time 0.07 seconds
cpu time 0.01 seconds

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

At the end of the day, which language you use is entirely up to you. R is free so that's helpful for sure.

 

My issue with R is the many, many different packages and ways of doing things. If you're doing basic processing you shouldn't have too many issues in either languages. Both have many free resources online to teach you the languages.

For SAS you can start here:

support.sas.com/training/tutorial

 

It has a free e-course, see bottom right hand corner, for the link.

 

I honestly can't recall if I worked with Pascal back in the day or not...I've played around with too many languages 😞

 

data Response;
infile cards dlm='09'x;

input ID var1 var2 var3 var4 var5;
cards;
1	3	4	5	2	3
2	3	4	5	1	3
3	3	4	4	3	4
4	3	4	5	1	3
5	4	5	5	5	5
6	4	5	5	5	5
7	3	4	4	3	4
8	4	1	2	4	6
9	3	4	5	2	3
10	4	5	5	5	5
;
run;

*sort into order;
proc sort data=response;
by var1-var5;
run;

*Identify each new group of variables;
data want;
set response;

by var1 var2 var3 var4 var5; *specify how data is sorted;

if first.var5 then class+1; *identify unique groups;

run;

*print to display;
proc print data=want;
run;

View solution in original post

24 REPLIES 24
art297
Opal | Level 21

It's easy to turn that into an array, but why do you want to do that? What are you trying to accomplish?

 

Art, CEO, AnalystFinder.com

 

Amanda_Lemon
Quartz | Level 8
Thank you for your prompt response. I need to be able to work with the cells.

Specifically, I have a dataset of students' responses and corresponding teacher responses. The teacher responses, however, are not about a student but about a class. Some students were in the same class and thus have the same corresponding teacher responses. I want to identify class ID's by comparing teacher responses (if they are the same, the ID would be the same).

I have a good idea how to accomplish that and already drafted a syntax using arrays. The problem is -- I can't turn the dataset into an array... Once I do that, I will be good to go.

Thank you.
art297
Opal | Level 21

That's the way an array looks in SAS (other than with SAS IML).

 

One can still process it as a mutlti-dimensional array, but there is probably a much easier way to do what you want.

 

What defines teacher in your data and what do you want your resulting data to look like?

 

However, to answer your question, the first datastep below creates some test data, while the second loads that data into a 50x6 array. If you look at the resulting log (after you run the code), you'll see that all of the array elements can be seen and, if desired, processed. However, that is typically a very inefficient way to use SAS:

data have (drop=j);
  input var1-var5;
  array vars(5) var1-var5;
  do id=1 to 50;
    if mod(id,2) then do;
      do j=1 to 5;
        vars(j)=vars(j)-1;
      end;
      output;
      do j=1 to 5;
        vars(j)=vars(j)+1;
      end;
    end;
    else output;
  end;
  cards;
2 3 4 5 2 3
;

data want;
  set have end=eof;
  array vars_in(5) var1-var5;
  array vars(50,6) _temporary_;
  vars(_n_,1)=id;
  do i=1 to 5;
    vars(_n_,i+1)=vars_in(i);
  end;
  if eof then do;
    do i=1 to 50;
      put vars(i,1)= vars(i,2)= vars(i,3)= vars(i,4)= vars(i,5)=;
    end;
  end;
run;

 

Art, CEO, AnalystFinder.com

Cynthia_sas
SAS Super FREQ
Hi:
And for a good explanation of two-dimensional arrays and how the variables get created, please take at look at Example 4 starting on page 9 in this paper: https://support.sas.com/rnd/papers/sgf07/arrays1780.pdf

The relevant information is at the top of page 9, where it says: "You can think of a two-dimensional array as having 'rows' and 'columns', but to SAS, a two-dimensional array is no different than a one-dimensional array. It is still an alias for data set variables in the PDV or for temporary data elements."

It's a good paper and sheds light on how you work with multi-dimension arrays.

cynthia
Reeza
Super User

You're going about this incorrectly. You don't need to use arrays here. I would strongly suggest posting sample data for what you're trying to do and someone can show you how to arange your data to obtain the results. It's best to use SAS 'way' of doing this rather than trying to fit it into another languages concepts. The array method is easier to think about because that's how we're taught math but it's not applicable here.

 

To join by ID you would do a MERGE or JOIN on the two datasets and then you would be able to make your comparisons. 

 

You probably can load your data into IML to work with it as a matrix but that's not how SAS processes data, it moves through the data line by line. 

 

 

Amanda_Lemon
Quartz | Level 8

Hi all,

 

Thank you for your responses. 

 

I feel that I am going in a wrong direction... Using the syntax of Art (thank you, Art!), I got this (goes on to 50):

 

pic.jpg

 

And that's not exactly what I want -- I want a 10x5 array, not 10x50 -- I played around but couldn't get 10x5...

 

Reeza, I am attaching the sample data. So, I have 10 students. var1-var5 are teacher responses about the class a student was in. Some students were in the same classes. For students who were in the same classes, teacher responses are the same. Task: to identify class IDs (i.e., students in the same classes will have the same class IDs). I put the correct answer - the correct class IDs - in the sample data file attached, so that you know what result I am looking for. 

 

How should I approach this? 

 

P.S. I learned Pascal years ago and the array method worked well there. Now I am trying to apply Pascal logic to SAS... but apparently it's not working... Maybe I should use a different programming language in which I can use the array method? Will R be suitable for that? 

art297
Opal | Level 21

You haven't said what you want to do with the data. Assigning class numbers is simple. I used the following code. My assigned numbers are different than yours, but follow the same pattern:

proc import datafile='/folders/myfolders/example.xls' dbms=xls out=have replace;
run;

proc sort data=have out=want;
  by var1-var5;
run;

data want;
  set want;
  by var1-var5;
  if first.var5 then class+1;
run;

But, I can't answer your question of what language would be better without knowing what you are trying to accomplish. I also used to program in Pascal (and Fortran, Basic and many other languages). My guess is that you will find SAS to be a lot more powerful than any of them.

 

Art, CEO, AnalystFinder.com

 

Reeza
Super User

At the end of the day, which language you use is entirely up to you. R is free so that's helpful for sure.

 

My issue with R is the many, many different packages and ways of doing things. If you're doing basic processing you shouldn't have too many issues in either languages. Both have many free resources online to teach you the languages.

For SAS you can start here:

support.sas.com/training/tutorial

 

It has a free e-course, see bottom right hand corner, for the link.

 

I honestly can't recall if I worked with Pascal back in the day or not...I've played around with too many languages 😞

 

data Response;
infile cards dlm='09'x;

input ID var1 var2 var3 var4 var5;
cards;
1	3	4	5	2	3
2	3	4	5	1	3
3	3	4	4	3	4
4	3	4	5	1	3
5	4	5	5	5	5
6	4	5	5	5	5
7	3	4	4	3	4
8	4	1	2	4	6
9	3	4	5	2	3
10	4	5	5	5	5
;
run;

*sort into order;
proc sort data=response;
by var1-var5;
run;

*Identify each new group of variables;
data want;
set response;

by var1 var2 var3 var4 var5; *specify how data is sorted;

if first.var5 then class+1; *identify unique groups;

run;

*print to display;
proc print data=want;
run;
Reeza
Super User

And sometimes you'll get answers that are the exact same seconds apart here. 

I'm impressed, we even referenced the variables the same way @art297

Amanda_Lemon
Quartz | Level 8

Thank you all! Dear Reeza and Art, when running your script, I get this error in the log: 

 

81 data want;
82 set example;
83 by var1 var2 var3 var4 var5;
84 if first.var5 then class+1;
-
400
ERROR 400-185: The SUM statement requires numeric expression.

85 run;

 

I am sure there is a very easy fix to that but I can't figure out... Could you please help me with that? Thank you in advance. 

Reeza
Super User

@Amanda_Lemon wrote:

Thank you all! Dear Reeza and Art, when running your script, I get this error in the log: 

 

81 data want;
82 set example;
83 by var1 var2 var3 var4 var5;
84

if first.var5 then classCount + 1;


-
400
ERROR 400-185: The SUM statement requires numeric expression.

85 run;

 

I am sure there is a very easy fix to that but I can't figure out... Could you please help me with that? Thank you in advance. 


I suspect you already have a variable called CLASS in your dataset which is a character variable. Pick any other variable name, that you don't have in your data set and change CLASS to that. 

 

 

Amanda_Lemon
Quartz | Level 8
WOW, it works now! Yes, that was the problem. Thank you!

Last quick question -- just to make sure I understand the syntax -- what does "if first.var5" exactly mean? What does SAS do when it sees this?

Thank you again!
Reeza
Super User

@Amanda_Lemon wrote:
WOW, it works now! Yes, that was the problem. Thank you!

Last quick question -- just to make sure I understand the syntax -- what does "if first.var5" exactly mean? What does SAS do when it sees this?

Thank you again!

Every time it encounters a new value of var5 that (first value of var5) in a group it increments the counter. 

Amanda_Lemon
Quartz | Level 8
Wait, why is it var5 that has a new value? I thought it should be that if the whole set (var 1 through var 5) is new, then increment the counter... no?

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
  • 24 replies
  • 3271 views
  • 5 likes
  • 6 in conversation