Hi all, using sas 9.4 on a windows platform (coming to SAS from C/C++ and Python background):
I'm using an array to compare a string variable with a list of strings to set a flag variable. This code works for a single variable, and it works in a different dataset (basically the same code) but when I try to use it with this dataset I am not getting any results.
Here's the main idea (pseudo-sas):
if this_one_code IN: (<list of strings>) then do;
flag = 1;
date = this_date
end;
/* The above works fine and the flags are set for valid variables */
/* now I try the same over the other codes: */
array dx(24) code2-code25;
do i = 1 to 24;
if dx[i] IN: (<list of strings>) then do;
flag = 2;
date = this date;
When I hit this array though, I get errors of "character values converted to numeric" for every conditional inside the array do block.
It works without the dollar sign in another program using the same code and string variable lists. I thought SAS would be smart enough to get the string implicitly but I guess that's not the case.
I tried correcting this by adding the $ before the variable:
array dx(24) $code2-code25;
This silences the error, but I get no results from the logic in the loop.
What I would like to know is:
Is this array declaration for an array of strings correct? If not, what should I do to correct?
Are there any other noticable issues?
Thanks.
The symptoms you describe are consistent with CODE2 - CODE25 being the wrong variable names. Perhaps in the original data they were something similar, like CODE_2 - CODE_25. Use whatever names are actually in your data set.
For an array with character variables you do need to add the $ sign in the array statement.
The date line of the code is incorrect, but I don't know if that's pseudo code or real code.
I don't think you've given us enough to answer your question.
Your full array isn't shown - there's no end? Is there an else?
If there's an error include.
Ideally, post some data that allows us to replicate the issue.
Another formula that's useful for this is WHICHC(). And another method is to use a format to check the diagnosis list.
array dx(24) code2-code25;
do i = 1 to 24;
if dx[i] IN: (<list of strings>) then do;
flag = 2;
date = this_date;
end;
end;
/* I repeat the above about seven times with a different list of strings*/
Edit: The formatting is broken above. The ends should be on newlines
The array ends after a bunch of conditionals as shown above.
However, I did as you suggested and made up some code for testing and it appears to run fine:
data code;
length code1 code2 code3 code4 $2;
input code1 code2 code3 code4;
datalines;
A1 A2 A3 A4
B1 B2 B3 B4
C1 C2 C3 C4
;
data code_check (drop=i);
set code;
flag1 = 0;
flag234 = 0;
if code1 IN: ("A1", "B1", "C1") then flag1 = 1;
array dx(3) $code2-code4;
do i = 1 to 3;
if dx[i] IN: ("A2", "B2") then flag234 = 1;
end;
run;
I wonder if I might be dealing with a data issue instead now. I will have to take a look.
The symptoms you describe are consistent with CODE2 - CODE25 being the wrong variable names. Perhaps in the original data they were something similar, like CODE_2 - CODE_25. Use whatever names are actually in your data set.
It's almost like you knew...
It was missing the underscore before the number.
I'll smack my head accordingly. Thanks!
Is there any way to get an error like that to show up? I'd think SAS would complain that the variable iterated over doesn't exist. Definitely didn't show up in any output.
There's no error because the array statement can also be used to create new variables. So your array statement was referring to new variables but not the ones you wanted.
The Log might have given you a clue with the number of variables in the ouput set. The log would have the number variables and if you notice there are 24 more than expected...
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.