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;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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