Hi...I'm teaching myself SAS and am new to this community board.
I am trying to recode 12 categorical variables (pbc_1-pbc_12) with the response "yes", "no", and "unsure". In my code, "yes" = 2, "no" = 1, and "unsure" = 3. I want to only capture the "yes" responses from the 12 variables and create one variable (precon). I'm using "else if" statements, but the code is not working. I also don't know if I am accounting for the "no" and "unsure" responses correctly. I essentially want to examine the distribution of "yes" responses.
precon=.;
if pbc_1 = 2 then precon = 1;
else if pbc_2= 2 then precon = 2;
else if pbc_3= 2 then precon = 3;
else if pbc_4 = 2 then precon = 4;
else if pbc_5= 2 then precon = 5;
else if pbc_6 = 2 then precon = 6;
else if pbc_7 = 2 then precon = 7;
else if pbc_8 = 2 then precon = 8;
else if pbc_9 = 2 then precon = 9;
else if pbc_10 = 2 then precon = 10;
else if pbc_11 = 2 then precon = 11;
else if pbc_12= 2 then precon = 12;
else precon = .;
Please help any way you can. Also, if there is a more sophisticated way of doing this type of procedure please let me know. Thanks! Trace
What do you mean by "not working"? What does a sample of your data look like? And what values would the resulting new variable take for that sample? Do you ever have more than one "yes" in the 12 variables? If so, what rule will you use to make the new variable?
Wanting to look at the "distribution of yes" responses suggests to me that, instead of finding a single summary variable, you may want to do cross-tabulations of the 12 original variables - this assumes that the "yes" values are not mutually exclusive. Or perhaps you can make 12 zero/one variables and generate a 12*12 correlation matrix from them? I guess I'm asking what is it that you want to be able to say about these data?
Better post some sample data and result.
Here could give you a start.
data want;
set have;
array x{*} pbc_1-pbc_12;
precon = whichn(2,of x{*});
run;
It is not clear if you want a separate result if you have only 1 variable with the value of 2 or multiple variables with the value of 2 or what to do with your 'no' or 'unsure' at all.
The code you have written will stop testing and assign a value to precon for the first match. Was that your intent?
And distribution of "yes" responses is also kind of vague.
"Not working" is awful vague.
Are there errors in the log?: Post the code and log in a code box opened with the {i} to maintain formatting of error messages.
No output? Post any log in a code box.
Unexpected output? Provide input data in the form of data step code pasted into a code box, the actual results and the expected results. Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the {i} icon or attached as text to show exactly what you have and that we can test code against.
Here is one way to search for a single specific numeric value from a group of variables. The order the variables appear in the array statement will affect results.
If you don't like the result of 0 when no match then add code to assign the value you want. This also will count the number of yes responses for a record.
data want; set have; array pb pbc_1-pbc_12; precon = whichn(2,of pb(*); Numyes = countc('2',catx(' ',of pb(*)); run;
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.