BookmarkSubscribeRSS Feed
tathagatab
Calcite | Level 5

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

11 REPLIES 11
WarrenKuhfeld
Rhodochrosite | Level 12

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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 11 replies
  • 940 views
  • 3 likes
  • 6 in conversation