How to set code "Y" if ABC = "R" else set to "N".
newvar = ifc(abc='R', 'Y', 'N');
@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;
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;
@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;
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.
How to write code in DATA SET =XYZ
if a = "B" THEN X = "Y" ;
if a ne."B" THEN set X = "N" ;
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;
HOW TO SELECT to time part of A where B = ("C" and "D")
I will merge your meaningless posts here until you start answering to our posts and/or post meaningful questions.
Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.
Register today!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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: