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;
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;
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;
@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.
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;
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.
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;
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 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.