I have five survey variables, similar in naming convention: ABC1, ABC2, ABC3, ABC4, and ABC5. Responses for these variables are either 1 or 2.
I want to shorten/simplify my code of:
If ABC1=1 and ABC2=1 and ABC3=1 and ABC4=1 and ABC5 =1 then CODE=8;
How do I simplify that code? In my real scenario, my last variable is ABC100.
Thank you!
The code you posted treats your data as numeric not character.
Use WHICHC() instead of WHICHN() then.
Documentations on functions are available here @Phil_NZ
You may also want ti book mark the formats and informats by category page (find them from the main documentation.sas.com page).
@Bluekeys49 wrote:
My variables ABC1 through ABC100 are defined as character ...
Then why not use character functions (this code works for numeric variables as well):
if cats(of abc:)=repeat('1',99) then code=8;
[Edit: Actually it's more robust for numeric variables because then the effect cats('11',' ')=cats('1','1') could not happen, assuming the standard setting of system option MISSING.]
@Bluekeys49 wrote:
My variables ABC1 through ABC100 are defined as character so I get an error.
Bad programmer! No cookie! 😞
If ABC1=1 and ABC2=1 and ABC3=1 and ABC4=1 and ABC5 =1 then CODE=8;
How do I simplify that code? In my real scenario, my last variable is ABC100.
if nmiss(of abc:)=0 and min(of abc:)=1 and max(of abc:)=1;
Sorry, @Bluekeys49 , I just stop by and feel interesting in the code
Could you please tell me how to search the documents to learn about
sum(of...) or nmiss(of...), or is there any document in your head that you think can help me to learn about these nice functions (with of inside bracket)?
Thank you!
If your variables are numeric you can test the RANGE of values. If the Range is 0 all of the values are the same.
So you test the range and a single (non-missing) variable for its value. If it is 1 then all the values are 1.
if range (of ABC: )=0 and Abc1=1 then code=8;
The ABC: uses all the variables whose names start with ABC. Or use range(of Abc1-Abc100) to use sequentially numbered variables. If there is a gap in the numbers, maybe you don't want to use ABC49 for some particular reason, you can provide two (or more) similar lists: range(of abc1-abc48, of abc50-abc100);
Note: Range will ignore missing values in the calculation. So if the requirement is to have no missing value you could add:
and nmiss(of abc:)=0.
You might also consider coding things as 1/0 instead and treat 1 as the condition most often of interest (Yes) perhaps.
Then an "all 100 variables must be exactly 1" becomes: sum(of abc1-abc100)=100
If you want to know that at least one variables has the value 1 (in this 1/0) coding scheme: max(of abc1-abc100)=1
At least one of the variables has a 0 : min (of abc1-abc100)=0.
Percent of variables with responses that answered 1 : mean(of abc1-abc100) yields the decimal percentage such as .125 =12.5%
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.