BookmarkSubscribeRSS Feed
emaguin
Quartz | Level 8

Both versions, run separately, run without errors but fail to function as intended. I could certainly write out a longer version but what's wrong here? I run through the array and reset the value of the variable if the current value equals the test value but the test value depends on the current element in the array. What's happening is that any variable in the array that has a value of 3 is reset to .p. 

 

array vrra{31} Drr1-Drr31;

*******  Version 1;

 do i=1 to 31;
   if (i le 11 and vrra{i} eq 3) then vrra{i}=.p;
   else if (i in (12, 19, 21, 27) and vrra{i} eq 6) then vrra{i}=.p;
   else if (i in (28, 29, 30, 31) and vrra{i} eq 7) then vrra{i}=.p;
end;

 

******** Version 2;

do i=1 to 31;
if (i le 11 and vrra{i} eq 3) then vrra{i}=.p;
if (i in (13-18, 20, 22-26) and vrra{i} eq 3) then vrra{i}=.p;
if (i in (12, 19, 21, 27) and vrra{i} eq 6) then vrra{i}=.p;
if (i in (28, 29, 30, 31) and vrra{i} eq 7) then vrra{i}=.p;
end; 

 

Here the follow up question. spss is my first stats program and in there i could do this.

do repeat x=Drr1 to Drr5/y=3 3 6 7 3.

+   if (x eq y) x=9. /* let 9 stand for .p.

end repeat.

 

Maybe sas has something like that, or not, i don't know. But what about this? Can something like this be done?

array vrra{5} Drr1-Drr5;

array temp{5} 3 3 6 7 3;  /* so temp is a temporay array that disappears along with its contents after run execution */

do i=1 to 5;

   if (vrra{i} eq temp{i}) vrra{i}=.p;

end;

run;

 

Thanks, 

Gene Maguin

3 REPLIES 3
Reeza
Super User

Both versions, run separately, run without errors but fail to function as intended.

I may have missed it in your post but it's not clear what you 'intend'. 

 

Can you show some sample input data and what you expect as output?

 

 


@emaguin wrote:

Both versions, run separately, run without errors but fail to function as intended. I could certainly write out a longer version but what's wrong here? I run through the array and reset the value of the variable if the current value equals the test value but the test value depends on the current element in the array. What's happening is that any variable in the array that has a value of 3 is reset to .p. 

 

array vrra{31} Drr1-Drr31;

*******  Version 1;

 do i=1 to 31;
   if (i le 11 and vrra{i} eq 3) then vrra{i}=.p;
   else if (i in (12, 19, 21, 27) and vrra{i} eq 6) then vrra{i}=.p;
   else if (i in (28, 29, 30, 31) and vrra{i} eq 7) then vrra{i}=.p;
end;

 

******** Version 2;

do i=1 to 31;
if (i le 11 and vrra{i} eq 3) then vrra{i}=.p;
if (i in (13-18, 20, 22-26) and vrra{i} eq 3) then vrra{i}=.p;
if (i in (12, 19, 21, 27) and vrra{i} eq 6) then vrra{i}=.p;
if (i in (28, 29, 30, 31) and vrra{i} eq 7) then vrra{i}=.p;
end; 

 

Here the follow up question. spss is my first stats program and in there i could do this.

do repeat x=Drr1 to Drr5/y=3 3 6 7 3.

+   if (x eq y) x=9. /* let 9 stand for .p.

end repeat.

 

Maybe sas has something like that, or not, i don't know. But what about this? Can something like this be done?

array vrra{5} Drr1-Drr5;

array temp{5} 3 3 6 7 3;  /* so temp is a temporay array that disappears along with its contents after run execution */

do i=1 to 5;

   if (vrra{i} eq temp{i}) vrra{i}=.p;

end;

run;

 

Thanks, 

Gene Maguin


 

PGStats
Opal | Level 21

There is a syntax error in your code

 

13-18, 20, 22-26

 

should be

 

13:18, 20, 22:26

PG
ballardw
Super User

An example of @PGStats comment about the syntax of the IN  list you are using in the version 2:

data example;
   do i= 1 to 10;
      inlist= ( i in (2-5,7, 8-10));
      rightlist = ( i in (2:5,7, 8:10));
      put i= inlist= rightlist=;
   end;
run;

Inlist and Rightlist will have a value of 1 when I is found in the list. Note that only the first of values in the hyphenated are matched for Inlist.

 

With a smaller set of variables but the same type of code as your version 1 a cannot duplicate getting "all values of 3" set to .P.

So either the code you ran is different than shown or perhaps you have other code assigning values of P, or copying from one of the affected variables.

Since you did not show 1) an entire data step, 2) the log entry from running the code and 3) provided no test data it is hard to determine what may have happened to set "all values of 3".

 

array temp{5} 3 3 6 7 3;  /* so temp is a temporay array that disappears along with its contents after run execution */

 

A temporary array of that sort would look like

array t{5} _temporary_ (3,3,6,7,3);

Caution: temporary arrays are not reset at each iteration of the data step. So if change values inside the array they will be the new values for the next record(s).

 

From the values I am seeing I would hazard guess that something like certain question responses are being set where the same "not really useful for analysis value" like "Don't know" "Refused" or similar is set to .P.

I tend to that with custom informats when reading data most of the time. So none of this scattered array indexing is needed.

Or create three separate arrays for the purpose of recoding.

Variables can be assigned to multiple arrays if needed. So if your vrra is doing something else other than recoding then recode in separate arrays for groups of variables prior to the other stuff.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 945 views
  • 0 likes
  • 4 in conversation