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

I have 30 variables that I'm trying to check through to see if they each match one of several results. If any of those specific results are in even one of those 30 variables, I want a new indicator variable to be equal to 1, and equal to 0 otherwise. I don't care if it's the first variable checked or the last but I seem to only be able to get it to check the last variable for the result options I want. It doesn't give errors, but only shows a few with the new variable equal to 1 and they seem to all be ones where the result is in the D30 variable so I'm guessing I'm missing something that makes it check all of the variables and mark the new variable to 1 no matter which variable it is, but I can't figure out how to fix that. Thank you!

 

I have this:

data combinedmasternewvars;
set project.combinedmaster;


Array cd(30) $ D1-D30;
do i=1 to 30;
If cd (i) in ("A04" "A05" "A06") then present=1;
Else present=0;
end;
Run;

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

The ELSE always resets it to 0, even after it's been found once. Remove the ELSE and you should have the answer you expect.

 

data combinedmasternewvars;
set project.combinedmaster;

Array cd(30) $ D1-D30;
present=0;
do i=1 to 30;
If cd (i) in ("A04" "A05" "A06") then present=1;
end;
Run;

@Geoghegan wrote:

I have 30 variables that I'm trying to check through to see if they each match one of several results. If any of those specific results are in even one of those 30 variables, I want a new indicator variable to be equal to 1, and equal to 0 otherwise. I don't care if it's the first variable checked or the last but I seem to only be able to get it to check the last variable for the result options I want. It doesn't give errors, but only shows a few with the new variable equal to 1 and they seem to all be ones where the result is in the D30 variable so I'm guessing I'm missing something that makes it check all of the variables and mark the new variable to 1 no matter which variable it is, but I can't figure out how to fix that. Thank you!

 

I have this:

data combinedmasternewvars;
set project.combinedmaster;


Array cd(30) $ D1-D30;
do i=1 to 30;
If cd (i) in ("A04" "A05" "A06") then present=1;
Else present=0;
end;
Run;

 


 

View solution in original post

9 REPLIES 9
pink_poodle
Barite | Level 11
Is your dataset arranged well with D1, D2, D3 … D30 columns in sequence, or is jumbled like D2, D1, D30, D5, because then D1-D30 will look at just D1 and D30. To arrange in sequence, you can precede with a data step retaining the right sequence like data a; retain D1 D2 D3; set combinedmaster; run;
Geoghegan
Obsidian | Level 7
I think D1 is earlier in the dataset and then D2-30 are in order later in
the dataset. Would that make it just look at D30?
pink_poodle
Barite | Level 11
I think that would be ok. If array did not include all 30 variables, the do loop would complain that array subscript is out of range.
Patrick
Opal | Level 21

or this way.

data combinedmasternewvars;
  set project.combinedmaster;
  array cd {*} $ d1-d30;
  present=0;
  do i=1 to dim(cd) until(present=1);
    if cd[i] in ("A04" "A05" "A06") then
      present=1;
  end;
run;

 

Tom
Super User Tom
Super User

@Geoghegan wrote:
I think D1 is earlier in the dataset and then D2-30 are in order later in
the dataset. Would that make it just look at D30?

The order of the variables in the dataset does not matter.

 

It is the order of the variables in your ARRAY statement that matters and D30 is the last variable in the array statement.  Because you include the ELSE clause only the result of last element  in the array matters.  Remove the ELSE clause.  If you want the new variable coded as 1 or 0 instead of 1 or missing then set it to 0 before the DO loop.

 

Reeza
Super User

The ELSE always resets it to 0, even after it's been found once. Remove the ELSE and you should have the answer you expect.

 

data combinedmasternewvars;
set project.combinedmaster;

Array cd(30) $ D1-D30;
present=0;
do i=1 to 30;
If cd (i) in ("A04" "A05" "A06") then present=1;
end;
Run;

@Geoghegan wrote:

I have 30 variables that I'm trying to check through to see if they each match one of several results. If any of those specific results are in even one of those 30 variables, I want a new indicator variable to be equal to 1, and equal to 0 otherwise. I don't care if it's the first variable checked or the last but I seem to only be able to get it to check the last variable for the result options I want. It doesn't give errors, but only shows a few with the new variable equal to 1 and they seem to all be ones where the result is in the D30 variable so I'm guessing I'm missing something that makes it check all of the variables and mark the new variable to 1 no matter which variable it is, but I can't figure out how to fix that. Thank you!

 

I have this:

data combinedmasternewvars;
set project.combinedmaster;


Array cd(30) $ D1-D30;
do i=1 to 30;
If cd (i) in ("A04" "A05" "A06") then present=1;
Else present=0;
end;
Run;

 


 

ballardw
Super User

Maybe:

 

data combinedmasternewvars;
   set project.combinedmaster;
   Array cd(30) $ D1-D30;
   Present=0;
   do i=1 to 30;
       If cd (i) in ("A04" "A05" "A06") then do;
            present=1;
            leave;
       end;
   end;
Run;

Set the "not found" before checking the values.

The above with the LEAVE instruction tells SAS to exit the loop set by the Do i= ....; Because the Leave instruction is set to execute conditionally when one of the values is found, it stops searching for the other matches. If none of the variables matches then the not found is the value as it has not been overwritten.

 

Please note that the code I posted is in a text box opened on the forum using the </> icon so formatting of text is maintained a bit nicer. You could also post in a code box which is opened with the little "running man" icon. The boxes help separate question from code and make pasted text much easier to read (especially if code is indented), or in the case of examples of a text file keep the contents. The message windows on this forum will reformat text and in some cases may result in non-printable characters that mean code copied and pasted from the forum will not execute as expected or at all even when it appears correct.

andreas_lds
Jade | Level 19

Already mentioned: The else-part is causing the problem.

data combinedmasternewvars;
  set project.combinedmaster;

  array cd[30] D1-D30;
  present = 0;
  do i=1 to 30;
    if cd[i] in ("A04" "A05" "A06") then do;
      present=1;
      leave;
    end;
run;
Geoghegan
Obsidian | Level 7

Thank you everyone! a few of those variations worked and that makes sense why what I did was messing it up.

I appreciate it!

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
  • 9 replies
  • 2603 views
  • 9 likes
  • 7 in conversation