"Y" and "N", assuming it stands for Yes and No coding with character values is one of the things that can add a lot of work to code.
If you use numeric values with 1 for 'Y' and 0 for 'N' then you find a large number of things can be done quite easily.
"Any value of 1 (y)" if max(of <varaible list>) = 1
"All of values the same" range(of <variable list>) =0
"All values Y" if sum(of <variable list>)= n(of <variable list>)
"All values 0 (N)" max(of <variable list>)=0
"Any difference" range(of <variable list>)=1
"Any N" min(of <variable list>) =0
"How many Y" sum(of <variable list>)
"Percent of 1" mean(of <variable list>) (this will be a decimal version: .25 =25%)
"Percent of 0" 1- mean(of <variable list>)
And just because someone gives you a file with Y N is not a good excuse. SAS custom informats and formats can read the values into 1/0 and display "Yes" "No" or "True/False" or any desired text.
proc format; invalue YN (upcase) 'Y' =1 'N' =0 other = .; value yn 1='Y' 0='N' .=' ' ; value tf 1='True' 0='False' .='NA' ; run; data example; informat v1-v3 yn.; input v1-v3; datalines; Y N N y n N Y y n ; proc print data=example; format v1-v3 yn.; run; proc print data=example; format v1-v3 tf.; run;
Notice that the UPCASE option on the invalue, creating an informat Yn. to read such automagically adjusts the case. The Other option means anything not the expected y or n gets assigned a missing value so this helps prevent garbage results later.
Using whichc() should work.
data have;
x='N';
y='N';
z='N';
output;
y='Y';
output;
stop;
run;
data want;
set have;
if whichc('Y',x,y,z)>0 then var='Y';
else var='N';
run;
"Y" and "N", assuming it stands for Yes and No coding with character values is one of the things that can add a lot of work to code.
If you use numeric values with 1 for 'Y' and 0 for 'N' then you find a large number of things can be done quite easily.
"Any value of 1 (y)" if max(of <varaible list>) = 1
"All of values the same" range(of <variable list>) =0
"All values Y" if sum(of <variable list>)= n(of <variable list>)
"All values 0 (N)" max(of <variable list>)=0
"Any difference" range(of <variable list>)=1
"Any N" min(of <variable list>) =0
"How many Y" sum(of <variable list>)
"Percent of 1" mean(of <variable list>) (this will be a decimal version: .25 =25%)
"Percent of 0" 1- mean(of <variable list>)
And just because someone gives you a file with Y N is not a good excuse. SAS custom informats and formats can read the values into 1/0 and display "Yes" "No" or "True/False" or any desired text.
proc format; invalue YN (upcase) 'Y' =1 'N' =0 other = .; value yn 1='Y' 0='N' .=' ' ; value tf 1='True' 0='False' .='NA' ; run; data example; informat v1-v3 yn.; input v1-v3; datalines; Y N N y n N Y y n ; proc print data=example; format v1-v3 yn.; run; proc print data=example; format v1-v3 tf.; run;
Notice that the UPCASE option on the invalue, creating an informat Yn. to read such automagically adjusts the case. The Other option means anything not the expected y or n gets assigned a missing value so this helps prevent garbage results later.
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.