BookmarkSubscribeRSS Feed
deleted_user
Not applicable
Hello,

I have been searching but I don't see a simple explaination of how to simply load the values of a table column into a 1-D array....
13 REPLIES 13
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
Suggest searching the SAS support website at http://support.sas.com/ and search for keyword argument like "data step programming array" and consider using a DATA step to input your data file and declare an ARRAY.

Scott Barry
SBBWorks, Inc.
deleted_user
Not applicable
Hello vasudeva,

Here is how you could do it. The SQL is mandatory because you need to specify the size of an array in a data step and this needs to be known at compile time. Tell me if you need more help.

Regards,

Yoba

data T01_input;
do i=1 to 10; drop i;
x=ranuni(0);
output;
end;
run;

proc sql noprint;
select count(*)
into :array_Size
from T01_input;
quit;

data T02_test;
array myNumbers {&array_size} _TEMPORARY_;
* ARRAY LOADING;
if _N_=1 then do j=1 to number_of_obs;
set T01_input nobs=number_of_obs;
myNumbers{j}=x;
end;

* DO SOMETHING;
put "do something";

* LET US USE THE ARRAY;
do j=1 to dim(myNumbers);
y=myNumbers{j};
put y;
output;
end;
stop;
run; Message was edited by: yoba
deleted_user
Not applicable
Thanks....

The way SAS's documentation, and almost all the documentation on the web, describes arrays is completely confusing.

Why in the world would they give and example of 1-D arrays in the situation where you have only one observation for each variable? Since when does anybody every work with just one observation?

Let's say you have a datasets with 5 columns (variables: v1 - v5) and 100 observations (row),
Why don't they just say, you can load the dataset into an array COLUMN WISE by using a 2-D array (5,100) < v1, ...., v5> ?

Then you can reference any entry of the array in the usual fashion?

If I am correct in what I am saying, then some people (the ones writing this documentation) need to learn how to communicate.....
deleted_user
Not applicable
...Ah, I see what you are doing now.....

..... this is exactly what I was after.

If this is what is necessary to create a 1-D array in which the scalar variables can be accessed (i.e. "arrayname(i)") notation to choose the i-th component of the 1_D array, then ... actually... no comment. I don't want to insult the creators of SAS!
deleted_user
Not applicable
.... I can't believe this. It appears that one of the main reasons for using SAS in the first place is that it is data set orientated. So it seems like most of the time one will be dealing data sets with many variables (columns) with many observation for each variable (rows).
You mean to tell me arrays in SAS arn't constructed so that one can easily either put the whole table in an array and then proceed to easily access each entry, or easily put just one of the variables in an array, and then easily assess each of the entries?

Ex: Assume 2 variables with 10 observations --> 10x2 table.

data v1 v2
0 5
1 3
. .

Once cannot define array() and then access the first row and second column by array(1,2), which should be 5.

Or better yet: define 1Darray() and then assess the 2nd element in this 1-D array by 1Darray(2), which should be 3.

... if not, then wow! What an intuitive language.....I bet there aren't many mathematicians using a language like that... Message was edited by: vasudeva
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
Arbitrary limit of 3 consecutive rants attained - OP must wait for a reply before next rant.

sbb
JackHamilton
Lapis Lazuli | Level 10
I was thinking that if SAS is not suitable for his/her needs, s/he should find another software solution. Or perhaps some classes would be helpful.
deleted_user
Not applicable
If a mathematician was using SAS, I would expect them to be using IML, which does load a data set directly into a two dimensional array. If you don't have access to IML, I suggest you look into the POINT= option of the SET statement. It allows you to more through records without having to load the entire data set into an array first.
deleted_user
Not applicable
...Yes, the IML.

Here is the deal. The documentation for SAS data set arrays should explain how the notation array help(1) refers to a nx1 vector of observations (as a group), not the first component of the 1-D vector array help.
Further:

Say test has 3 variables v1 -v3 (columns) and 10 observations (rows):

data check;
set test_data;
array input(3) v1-v3;
array out(3)_temporary_;
. .do i=1 to 3;
. .out(i)=reps(1)*reps(i);
end;
%put out;
run;


Now, if you were new to SAS, you would assume out(i)=reps(1)*reps(i);
is the product of the scalar valued first entry in reps with the scalar valued ith entry in reps.
However, reps(i) refers to the vector of 10 observations in the ith column of reps. Further, even if you know rep(i) is a vector the line:
out(i)=reps(1)*reps(i);
appears to be a vector operation!!! (since out(i), reps(1), reps(i) are all 10x1 vectors)
However, SAS has the implicit itteration built into the data statment. Assume this itteration is on the first row of test_data, so that _N_=1. Even though,
we appear to be referencing vector quantities in: out(i)=reps(1)*reps(i);
SAS actually still performs the operation in: out(i)=reps(1)*reps(i)
entry by entry corresponding to the rows of the original dataset.

In other words, lets say we have all 1's in test_data, then chronologically, this how SAS populates array out:

1 1 1 ' ' ' ' 1 1 1 ' ' ' ' 1 1 1
. . . ' ' ' ' ' ' 1 1 1 ' ' ' ' 1 1 1
. . . ' ' ' ' ' ' . . . ' ' ' ' ' ' 1 1 1 and so on.....

---- time --->

(the .'s are blank entries in the array
the ' are just for space)



I do not think it is clear at all, that SAS should operate this way. Basically, SAS has preprogramed opperations going on behind the surface, and notation which looks similiar to common notation in other languages, including Math, but which actually means something different.........

Message was edited by: vasudeva

Message was edited by: vasudeva

Message was edited by: vasudeva

Message was edited by: vasudeva Message was edited by: vasudeva
LAP
Quartz | Level 8 LAP
Quartz | Level 8
Thought I would add my two cents here, if it really matters.

The SAS documentation is very clear about what an array is or is not in SAS

From the documentation...

"Arrays in the SAS language are different from arrays in many other languages. A
SAS array is simply a convenient way of temporarily identifying a group of
variables. It is not a data structure, and array-name is not a variable."

For what it's worth....
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
Clearly you are adapt at mixing MACRO and DATA step programming code, which in the end, yields suspect and likely invalid results. To continue with your bogus rants is fairly certail to gain little to no benefit from the forum. You've made your point about dis-satisfaction for SAS documentation on various topics....so be it...move on compadre!!

And if you have a distinct SAS programming challenge or concern, I'd suggest you void yourself of this particular thread and open a new one in a particular forum for meaningful participation and attention from other forum subscribers.

In a nutshell, it's time to move on to another topic, Bro!!!

Scott Barry
SBBWorks, Inc.
deleted_user
Not applicable
Okay, I am letting it go, just one more comment.

Yeah, I did see the first part of that in the documentation..... a little more explaination would be good, though....

.... regarding letting it go, I just included the last post to try to explain where the confusion comes from since it is not clear on the surface. I have spoken to several general programmers and they had the same first instincts as I did.

I see the utility of SAS doing it this way. It is cool...
AjayKant
Calcite | Level 5

Just run this and verified is this what you wanna ;

proc sql;

create table work.one

(X num format=1.,A CHAR(1));

insert into work.one

values(1,'a')

values(4,'d')

values(2,'b')

;

quit ;

proc sql noprint  ;

select x into :X1-:X3

from one ;

quit ;

%put _global_ ;

*Look for X1 X2 X3 macro values ;

Please confirm .

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
  • 13 replies
  • 8015 views
  • 0 likes
  • 5 in conversation