How to use a column's value in array index in SAS - arrays

I want to subset a dataset based on the value of a column from a set of columns.
For ex,
I want to subset the dataset by age>50 but i have 10 age columns ranging from age1-age10. Also, there is another column named identifier (with values from 1-10) which tells me based on what column should i subset the value.
Let's say my identifier column value is 5, I should check whether the value in column age5 is greater than 50 or not.
This is what I tried, But it's not working.
data library.table2;
set library.table1;
array age[10] age1 -- age10;
if( age(identifier)>50 );
RUN;

The list of variables meant by a positional variables list like age1 -- age10 could be completely different than the variable list meant by a simple enumerated list like age1 - age10. The former looks for all variables BY POSITION between AGE1 and AGE10. It could include 2 variables or 200 variables. It might include some character variables.
If you want AGE1,AGE2,...,AGE10 then you can either list the variables or specify the dimension. If you do not list the actual variable names in some way then it will just append an index number to the name of the array to generate the variable names for the array.
array age age1-age10;
array age[10] ;
If you are a belt and suspenders type of person you can specify both the dimension and the specific variable names.
array age[10] age1-age10;
You might also want to check that IDENTIFIER is a valid index.
if identifier in (1:10) then if not age(identifier)>50 then delete;

Related

Get name of dimensional array

I am currently trying to match a HyperArray parameter to the database.
I am trying to get the match the Dimension name to the database name. I am struggle to call/get the Dimension name. Does anyone have advise on how I could call the dimension name if I select Dimension 1.
Modules is a HyperArray Parameter
Database
Previous Assistance Received
as you know, a variable can have multiple dimensions, and each dimension can have multiple values, where each value is associated to a name.
There are 2 indexes of importance, the first one is the one associated to the index of the dimension, let's call it dimIndex
In your case there's only 1 dimension called Module_List so, dimIndex must be equal to 0
The second one is the value of that dimension, in your case Module_List has values of Busman_113 and Busman_124
Modules.getDimensions()[dimIndex].getIndexName(0) will be equal to "Busman_113"
and
Modules.getDimensions()[dimIndex].getIndexName(1) will be equal to "Busman_124"

Excel array formula - managing data in table with dynamic height

I have a working array formula, which creates a list of values in column A. The length of the list depends on the input values of the formula and will vary over time. Values could be removed and added anywhere in the list, not necessarily at the end. Now in column B I want to manually add comments on the values in column A. When the length of the list changes, the values in column B don't move along with the values in column A, so they are not longer on the correct line. Is there a way to solve this?
You can try to use the OFFSET function for the dynamic variable.

Retrieving data from table using cell array - Matlab

I have a table in Matlab crsp and and cell array of numbers that serve as keys. I want to retrieve information from the table using that cell array which is stored as a variable. My code is as follows:
function y = generateWeights(permno_vector, this_datenum, crsp)
crsp(crsp.PERMNO == permno_vector,:);
crsp is defined as a table while permno_vector is the cell array. It contains a couple permnos that are used to retrieve information.
In this case, my code is not working and will not allow me to access the values in crsp. How do we access table values using a vector array?
As James Johnstone points out, the first problem with the code you've posted is that it doesn't assign anything to y, so as written your function doesn't return a value. Once you've fixed that, I assume the error you are seeing is Undefined operator '==' for input arguments of type 'cell'. It's always helpful to include this sort of detail when asking a question.
The syntax
crsp(crsp.PERMNO == x,:)
would return those rows of crsp that had PERMNO equal to x. However if you want to supply a list of possible values, and get back all the rows of your table where your target variable matches one of the values in the list, you need to use ismember:
crsp(ismember(crsp.PERMNO, cell2mat(permno_vector)),:)
if permno_vector is a cell array, or simply:
crsp(ismember(crsp.PERMNO, permno_vector),:)
if you can instead supply permno_vector as a numeric vector (assuming of course the data in crsp.PERMNO is also numeric).

What is the difference between array and enum in C ?

I find enum to be similar to an array,with the elements in it being numbered from 0. So what is the difference between an array and enum?
An Enum is basically a group of named constants.
It is an alternative to numbered flag parameters.
(It also doesn't have to be numbered from zero, you can specify the numbering.)
An Enum could be days of the week for example.
A Enum could departments in a company: eg SALES, BILLING, HR ...
A array is a sequence of memory locations. It is a collection. Each element in that collection is indexed by a number. So using that number you can retrieve the value stored at that location. Much like the page number in a book lets you look up the content of that page, the index on an array lets you look up the value stored at that location.
For example, if your company had numbered physical mail boxes for each department (starting from zero):
and you were creating some very simple software to let users log in to check how many letters they had uncollected,
you might choose to store them in an Array of Ints. Where the index is the mail box number, and the value is the number of letters in the box.
Then for ease of programming, you might choose define the departments as Enums (as described before) such that you could index of department name. But that would be getting more advanced.
You may be confusing Enum, with Enumerable which is a term used in some languages to describe any collection type which can be iterated though in sequence (Eg IEnumerable in C#). This term is not often used in C. It is not in anyway the same at all as Enum.
Array is a variable that can contain multiple elements with index starting from 0 whereas enum is an user defined datatype that contains a list of members for which an integer constant is assigned starting from 0. in case of enum the numbers starting from 0 are not indexes whereas in case of an array they are indexes. Also in case of enum you can assign your own constant values to the members that may or may not start from 0 and may or may not be in a sequence.
The main difference is that an array is a value and an enum is a type. And One main difference we can say that an array is a collection of other values (that is it contains other values, you can iterate through them or access individual ones by index), whereas an enum value is simply one atomic value. It doesn't contain any other values.
Both of them are totally different. Array is a value and enum is a type. array is a collection of different values whereas enum value is simply one value.
Array is used to iterate among various values using index whereas enum is assigned some atomic value and iterated so that we can easily iterate the type.
Array's value are assigned by using = operator 'eg. int a[4]={1,2,3,4}' and enum is the user defined data type and it is assigned like
enum days{sun,mon,tue,wed,thr,fri,sat}

Add a new records to a cell array matlab

I am using the following function in matlab:
getgenpept(AccessionNumber)
where the only parameter is a unique identifier. The problem is that I want to have a structure with around 50 different records all based on their unique identifier. Is there a way I can define a structure, and then add in my 50 different records as I go along, or ideally specify a list of identifiers before hand and loop the getgenpept() function in one go?
I want to end up with a cell array that stores each record in their own cell.
Hope this is clear!
If A is a cell array containing all the identifiers, then it's as easy as:
A = {'AAA59174', 'AAA59175','AAA59176'};
B = cellfun(#getgenpept,A);
B(1) is then the record for 'AAA59174', and so on.

Resources