BookmarkSubscribeRSS Feed
tathagatab
Calcite | Level 5

How to set code "Y" if ABC = "R" else set to "N".

11 REPLIES 11
WarrenKuhfeld
Ammonite | Level 13

newvar = ifc(abc='R', 'Y', 'N');

Tom
Super User Tom
Super User

@tathagatab wrote:

How to set code "Y" if ABC = "R" else set to "N".


The way to set a value to a variable is called an assignment statement.  But you don't say what variable you want set to Y.  Let's assume it is called CODE.  So you are talking about two assignment statements like:

code = 'Y';
code = 'N';

The way to conditionally run any statement is the IF/THEN/ELSE structure.  So you want to do:

if abc= 'R' then code = 'Y';
else code = 'N';

You will need to use these statements in a data step.  So assuming your current dataset is named HAVE you can use this step to make a new dataset named WANT with the new CODE variable created.

data want;
  set have;
  if abc= 'R' then code = 'Y';
  else code = 'N';
run;

 

tathagatab
Calcite | Level 5

HOW TO WRITE CODE IN NEW VARUABLE ABC: If Y = 'Z'  WHERE x = ‘o' then set to set to = 'Z' ; else if Y not missing where x = ‘o' then set to "L"; else if ARM not missing then set to "R". RUN;

Tom
Super User Tom
Super User

@tathagatab wrote:

HOW TO WRITE CODE IN NEW VARUABLE ABC: If Y = 'Z'  WHERE x = ‘o' then set to set to = 'Z' ; else if Y not missing where x = ‘o' then set to "L"; else if ARM not missing then set to "R". RUN;


I sounds like you want to determine what value to assign to ABC based on the values of Y and X and ARM.  Is that correct?

You list three combinations to check that do not cover all possible situations.  Are there any records that do not fall into one of those categories?  What do you want to set ABC to in those cases?  Do you just want to leave it alone?  If it is a new variable then it will be set to ' '.   Also if ABC is a new variable you really should tell SAS how to create it. Otherwise SAS will guess based on how it is first used.  So if ABC could ever have values that take more than one byte you would need to define it before the code that sets it to 'Z'. Otherwise SAS will guess you want to define it as length $1.

data want;
  set have;
  length abc $1;
  if y='Z' and x='o' then ABC='Z';
  else if not missing(y) and x='o' then ABC = 'L';
  else if not missing(arm) then ABC='R';
  else ;
run;
Kurt_Bremser
Super User

Please stop pestering us with near-identical questions about the same issue; instead study the answers given, and ask proper follow-up questions in the same thread.

tathagatab
Calcite | Level 5

How to write code in   DATA SET =XYZ

if a = "B" THEN X =  "Y" ;

if a ne."B" THEN set X =   "N" ;

Jagadishkatam
Amethyst | Level 16
Could you please provide a sample and expected output
Thanks,
Jag
PeterClemmensen
Tourmaline | Level 20

See if you can use this as a template

 

data have;
   a = "A"; output;
   a = "B"; output;
run;

data want;
   set have;
   if a = "B" then x = "Y";
   else            x = "N";
run;
tathagatab
Calcite | Level 5

HOW TO SELECT  to time part of A   where B = ("C" and "D")

 

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 11 replies
  • 1810 views
  • 3 likes
  • 6 in conversation