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

Hello, 

 

I have a range of numeric variables (txn_a, txn_b, txn_c...) in my dataset that I need to recode missing to zero depending each other's response. The condition is that if any variables in this range is NOT missng the others variables missing should be recoded to zero. 

 

Any ideas how can I can accomplish that with an array?

 

Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
Shmuel
Garnet | Level 18

Use a flag to assign an existing missing value in the array,

then, according to flag value, replace missing by zero:

 

array name txn_a ... ;
flag=0;
do i=1 to dim(name);
    if name(i)=. then do;
       do while (i le dim(name));
            flag=1;
            if name(i) = . then name(i) = 0;
            i+1;
        end;
        if flag=1 then leave;
end;
drop flag;

View solution in original post

3 REPLIES 3
Shmuel
Garnet | Level 18

Use a flag to assign an existing missing value in the array,

then, according to flag value, replace missing by zero:

 

array name txn_a ... ;
flag=0;
do i=1 to dim(name);
    if name(i)=. then do;
       do while (i le dim(name));
            flag=1;
            if name(i) = . then name(i) = 0;
            i+1;
        end;
        if flag=1 then leave;
end;
drop flag;
sofiacvp
Calcite | Level 5

I keep getting the following error:

 

2754 run;
-
117
ERROR 117-185: There was 1 unclosed DO block.

NOTE: The SAS System stopped processing this step because of errors.

 

Here is my code:

 

data gls.txrecoded;
set gls.secondarytasp;
array name (9) txsnum_k12 txsnum_hed txsnum_sab txsnum_jj txsnum_er txsnum_tstg txsnum_cw txsnum_mh txsnum_phc;
flag=0;
do i=1 to dim(name);
if name(i)=. then do;
do while (i le dim(name));
flag=1;
if name(i) = . then name(i) = 0;
i+1;
end;
if flag=1 then leave;
end;
drop flag;
run;

 

Any ideas?

Tom
Super User Tom
Super User

Since you have three DO statements and only two END statements the error message is pretty clear.

 

From your requirements you just need to do something like this.

data gls.txrecoded;
  set gls.secondarytasp;
  array name txsnum_k12 txsnum_hed txsnum_sab txsnum_jj txsnum_er txsnum_tstg txsnum_cw txsnum_mh txsnum_phc;
  if n(of name(*)) then do i=1 to dim(name);
      name(i)=sum(0,name(i));
  end;
  drop i;
run;

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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
  • 1021 views
  • 1 like
  • 3 in conversation