BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
tbendubois
Fluorite | Level 6

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.

 

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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

7 REPLIES 7
Reeza
Super User

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.

 

 

 

tbendubois
Fluorite | Level 6
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.

Astounding
PROC Star

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.

tbendubois
Fluorite | Level 6

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. 

Reeza
Super User

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.

tbendubois
Fluorite | Level 6
Good to know. Thanks for that explanation.
ballardw
Super User

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

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 7 replies
  • 815 views
  • 3 likes
  • 4 in conversation