BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
SASuserlot
Barite | Level 11
How I can achieve this!
I have 3 variables , x1,x2,x3 . These variables contains either "Y" or "N". I need to create a 4 th variable x4.
If any of the x1,x2,x3 contains "Y" then x4="Y".
1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

"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.

View solution in original post

2 REPLIES 2
Patrick
Opal | Level 21

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;

 

ballardw
Super User

"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.

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

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!

Register now

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 914 views
  • 2 likes
  • 3 in conversation