Hello, I was wondering if there was a way to check a range of variables to see if any are equal to a certain value.
data checks;
input check_1 $ check_2 $ check_3 $ check_4 $;
datalines;
Checked Unchecked Unchecked Unchecked
Unchecked Unchecked Unchecked Unchecked
Unchecked Checked Unchecked Unchecked
;
run;
Like for the above code, is there a way for me to test each line, and if any of the check_# variables equal to 'Checked' and save it to a new variable?
Thank you!
Please double check that your data step creates the values you expect. The input statement you used only had 8 characters for the values which meant values of "Unchecke" were in the data.
There are two functions that may be what you are looking for WHICHC for character values and WHICHN for numereric values. The function will return the position number of the first found match or 0 if not found.
data checks; input check_1 :$10. check_2 :$10. check_3 :$10. check_4 :$10.; result = whichc('Checked',of check_1 - check_4); datalines; Checked Unchecked Unchecked Unchecked Unchecked Unchecked Unchecked Unchecked Unchecked Checked Unchecked Unchecked ; run;
You did not indicate exactly what was to be stored in then new variable.
Awesome! Thank you so much!
What I had in mind was basically storing either True or False if Checked was in the range. But I think I can make my problem work with having result be greater than 0.
SAS will actually treat any numeric value other than 1 as "true" but this code may be more helpful:
result = ( whichc('Checked',of check_1 - check_4) > 0);
The () around every thing will force a logical comparison and return 1 for true if found and 0 for false.
Strongly recommend leaving it as numeric 1 and 0 as that is much easier to deal with than actual "True" or "False" values. You can use a custom format to display text of True/False if needed.
When it comes to reporting if you SUM a numeric 1/0 coded value then the total is the number "true", MEAN is the percent true a decimal i.e. .75 = 75% true. There are a few other tricks available as well but these two work will with Proc Report or Tabulate for nice reports.
You can convert "greater than 0" to a value of 1, with 1 indicating true and 0 indicating false.
data checks; input check_1 :$10. check_2 :$10. check_3 :$10. check_4 :$10.; array x{*} $ check_1 - check_4; result = 'Checked' in x; datalines; Checked Unchecked Unchecked Unchecked Unchecked Unchecked Unchecked Unchecked Unchecked Checked Unchecked Unchecked ; run;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.