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

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;

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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

6 REPLIES 6
data_null__
Jade | Level 19

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

Linlin
Lapis Lazuli | Level 10

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;

Reeza
Super User

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.

Astounding
PROC Star

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.

ArtC
Rhodochrosite | Level 12

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);

SASstudent2013
Calcite | Level 5

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.

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 6 replies
  • 6254 views
  • 7 likes
  • 6 in conversation