DATA Step, Macro, Functions and more

Error/No Results in String Comparison using Array

Accepted Solution Solved
Reply
New Contributor
Posts: 4
Accepted Solution

Error/No Results in String Comparison using Array

[ Edited ]

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.

 


Accepted Solutions
Solution
‎03-08-2017 02:56 PM
Super User
Posts: 5,500

Re: Error/No Results in String Comparison using Array

Posted in reply to tbendubois

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.

View solution in original post


All Replies
Super User
Posts: 19,782

Re: Error/No Results in String Comparison using Array

Posted in reply to tbendubois

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.

 

 

 

New Contributor
Posts: 4

Re: Error/No Results in String Comparison using Array

[ Edited ]
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.

Solution
‎03-08-2017 02:56 PM
Super User
Posts: 5,500

Re: Error/No Results in String Comparison using Array

Posted in reply to tbendubois

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.

New Contributor
Posts: 4

Re: Error/No Results in String Comparison using Array

[ Edited ]
Posted in reply to Astounding

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. 

Super User
Posts: 19,782

Re: Error/No Results in String Comparison using Array

Posted in reply to tbendubois

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.

New Contributor
Posts: 4

Re: Error/No Results in String Comparison using Array

Good to know. Thanks for that explanation.
Super User
Posts: 11,343

Re: Error/No Results in String Comparison using Array

Posted in reply to tbendubois

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...

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 143 views
  • 3 likes
  • 4 in conversation