Help using Base SAS procedures

Array error

Accepted Solution Solved
Reply
Contributor
Posts: 23
Accepted Solution

Array error

Hi. I need to update about 231 variables in a dataset so that the response is either a 1 or 0 for a specific number of observations. Right now, the response is a 1 or missing. To do this, I was going to setup an array in SAS so that the responses for all of the 231 variables would update to 0 if the original response was missing and a second variable response is 1. The variables that I want to update are "check1_5" through "check42_5". Variable "fu24yes" is a variable that denotes whether or not that person was part of the 24month followup (so that's the restriction). Do you know what I might be doing wrong?

Code:

data NewVar;

ARRAY MedZero (231) check1_5--check42_5;

DO i = 1 TO 231;

  IF MedZero(i) = . AND fu24yes=1 THEN MedZero(i) = 0;

  end;

run;

Log:

41   data NewVar;

42   ARRAY MedZero (231) check1_5--check42_5;

ERROR: Variable check1_5 cannot be found on the list of previously defined variables.

ERROR: Too few variables defined for the dimension(s) specified for the array MedZero.

43   DO i = 1 TO 231;

45     IF MedZero(i) = . AND fu24yes=1 THEN MedZero(i) = 0;

46     end;

47   run;


Accepted Solutions
Solution
‎03-25-2013 01:15 PM
Super User
Posts: 5,085

Re: Array error

The main problem is that you left out the SET statement in your DATA step.  There is no source of incoming data.

Secondarily, you could speed up the program by changing your DO loop:

if fu24yes=1 then do i=1 to dim(medzero);

   if MedZero{i}=. then MedZero{i}=0;

end;

Your program would be able to check fu24yes just once instead of 231 times, per observation.

Good luck.

View solution in original post


All Replies
Respected Advisor
Posts: 3,777

Re: Array error

You cannot create variables with a name range list (double dash).  

Super Contributor
Posts: 1,636

Re: Array error

data NewVar;

set your_dateset;

ARRAY MedZero(*) check1_5--check42_5;

DO i = 1 TO dim(medzero);

  IF MedZero(i) = . AND fu24yes=1 THEN MedZero(i) = 0;

  end;

run;

Super User
Posts: 17,867

Re: Array error

My guess is that you've miscounted the number of variables you're looking at or you have other variables in between check1_5--check42_5.

The -- operator specifies that you'll use all variables in between check1_5 and check42_5 and does rely on the order of the variables in the datastep.

Solution
‎03-25-2013 01:15 PM
Super User
Posts: 5,085

Re: Array error

The main problem is that you left out the SET statement in your DATA step.  There is no source of incoming data.

Secondarily, you could speed up the program by changing your DO loop:

if fu24yes=1 then do i=1 to dim(medzero);

   if MedZero{i}=. then MedZero{i}=0;

end;

Your program would be able to check fu24yes just once instead of 231 times, per observation.

Good luck.

Valued Guide
Posts: 632

Re: Array error

Another minor performance enhancement could replace the inner if:

     if MedZero{i}=. then MedZero{i}=0;

with an assignment statement and the coalesce function

     MedZero{i}=coalesce(MedZero{i},0);

Contributor
Posts: 23

Re: Array error

Thanks everyone for your replies. I've updated the code based on your feedback and it worked perfectly. One issue is that I had 231 as the number of variables when it should have been 232.

☑ This topic is SOLVED.

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

Discussion stats
  • 6 replies
  • 2459 views
  • 7 likes
  • 6 in conversation