Greetings,
I am trying to combine two sets of 233 variables into one in a data set. (I have data from two identical measures, each with 233 variables, that were recorded as separate columns based on whether the participant was contacted via text or email.) The variables have the prefix cbe_ or cbt_ based on which measure was used. So, for instance, I would ultimately like to have a third variable, cba_firstvar, that has values equal to those of cbe_firstvar for the cases who were contacted via e-mail and values equal to those of cbt_ firstvar for the cases who were contacted via text.
As a first step, I have been attempting to create the cba_ variables based on the cbt_ variables using an array. This is the code I have:
array textn {*}
cbt_firstvar
cbt_secondvar
cbt_thirdvar;
array combinedn {*}
cba_firstvar
cba_secondvar
cba_thirdvar;
do k=1 - dim(textn);
combinedn{k} = textn{k};
end;
When I run it, I receive this error message:
ERROR: Array subscript out of range at line 18763 column 16.
I cannot figure out why I am receiving this message since I believe the "dim(textn)" code is requesting SAS to define the upper bound of k.
I'm also unsure if this is the best strategy to achieve my ultimate goal. The data were collected in REDCap, so I can use the export function to separate it into two sets -- one with cases who were contacted via text and the other via e-mail. My current plan is to create the third variables with the cba_ prefix in each of the two separate data sets and then combine them.
(Also, I'm running SAS v9.4.)
Any help would be greatly appreciated. Thanks for your time and consideration!
Andrew
I guess you meant
do k=1 to dim(textn);
Your mistaken formila created a negative vaue in k.
I guess you meant
do k=1 to dim(textn);
Your mistaken formila created a negative vaue in k.
Thank you so much! I am new to using arrays. Can't believe it was such an obvious mistake.
The details:
A data step do allows the use of until() and while() conditions, increments/decrements (where you supply a termination value with "to", and the increment/decrement steps with "by"), and/or value lists. Your syntax ended up being a value list with a single value, which is syntactically valid during compilation, but caused a runtime error when the resulting value was used as an array index.
That makes sense. Thanks again! It worked when I made that correction.
This code works, but I noticed that it also creates a variable in the dataset named k (equivalent to the number of items in the array +1 for all rows of data). Is this to be expected?
Yes. A variable you create in the step will automatically be included in the output. Use a drop statement or a drop= dataset option to prevent that.
Thanks. I just wanted to be sure the creation of the variable didn't indicate that I had a problem in my code!
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.