Hello, I am trying to create multiple new variables, but when I create a variable using conditional statements it overwrites the variable that I previously created.
Example:
data a;
set Pathway.Tang_pathwayfinal;
if ER=1 and HER2_best=2 then subtype=1;/*luminal A*/
else if ER=1 and HER2_best=1 then subtype=2;/*luminal B*/
else if ER=2 and PR=2 and HER2_best=2 then subtype=3;/*Triple Negative*/
else if ER=2 and PR=2 and HER2_best=1 then subtype=4;/*Her2 Overexpression*/
else subtype=.;
run;
data a;
set Pathway.Tang_pathwayfinal;
if ER=1 then ERnew=1;
else if ER=2 then ERnew=2;
else ERnew=.;
run;
In this example the ERnew variable will overwrite the subtype variable in my dataset and I am not sure why. It seems that I am restricted to creating only one variable. I am using SAS v. 9.4.
Please help, Thank you!
It does overwrite the data set, but you essentially told it to.
Code 1:
Input data set = tang_pathwayfinal
output data set = A
data a;
set Pathway.Tang_pathwayfinal;
Code2 :
Input data set = tang_pathwayfinal -> NOT A?
output data set = A -> replaces A from above.
data a;
set Pathway.Tang_pathwayfinal;
In your first step you use the data set pathway to create a data set A
In the second step you use the data set pathway to create a data set A.
Because you didn't use that A data set again, it overwrites your previous data set, and nothing from that is carried forward.
In your second step, if you want to continue to work with that data use the newly created data set as your input data set.
Code 2 should be:
data A2;
set A;
....remaining code ....
run;
@liebera wrote:
Hello, I am trying to create multiple new variables, but when I create a variable using conditional statements it overwrites the variable that I previously created.
Example:
data a;
set Pathway.Tang_pathwayfinal;
if ER=1 and HER2_best=2 then subtype=1;/*luminal A*/
else if ER=1 and HER2_best=1 then subtype=2;/*luminal B*/
else if ER=2 and PR=2 and HER2_best=2 then subtype=3;/*Triple Negative*/
else if ER=2 and PR=2 and HER2_best=1 then subtype=4;/*Her2 Overexpression*/
else subtype=.;
run;
data a;
set Pathway.Tang_pathwayfinal;
if ER=1 then ERnew=1;
else if ER=2 then ERnew=2;
else ERnew=.;
run;
In this example the ERnew variable will overwrite the subtype variable in my dataset and I am not sure why. It seems that I am restricted to creating only one variable. I am using SAS v. 9.4.
Please help, Thank you!
It does overwrite the data set, but you essentially told it to.
Code 1:
Input data set = tang_pathwayfinal
output data set = A
data a;
set Pathway.Tang_pathwayfinal;
Code2 :
Input data set = tang_pathwayfinal -> NOT A?
output data set = A -> replaces A from above.
data a;
set Pathway.Tang_pathwayfinal;
In your first step you use the data set pathway to create a data set A
In the second step you use the data set pathway to create a data set A.
Because you didn't use that A data set again, it overwrites your previous data set, and nothing from that is carried forward.
In your second step, if you want to continue to work with that data use the newly created data set as your input data set.
Code 2 should be:
data A2;
set A;
....remaining code ....
run;
@liebera wrote:
Hello, I am trying to create multiple new variables, but when I create a variable using conditional statements it overwrites the variable that I previously created.
Example:
data a;
set Pathway.Tang_pathwayfinal;
if ER=1 and HER2_best=2 then subtype=1;/*luminal A*/
else if ER=1 and HER2_best=1 then subtype=2;/*luminal B*/
else if ER=2 and PR=2 and HER2_best=2 then subtype=3;/*Triple Negative*/
else if ER=2 and PR=2 and HER2_best=1 then subtype=4;/*Her2 Overexpression*/
else subtype=.;
run;
data a;
set Pathway.Tang_pathwayfinal;
if ER=1 then ERnew=1;
else if ER=2 then ERnew=2;
else ERnew=.;
run;
In this example the ERnew variable will overwrite the subtype variable in my dataset and I am not sure why. It seems that I am restricted to creating only one variable. I am using SAS v. 9.4.
Please help, Thank you!
Wow, what a silly mistake! Thank you so much, I am quite new to this. I appreciate your quick reply and help.
@liebera wrote:
Hello, I am trying to create multiple new variables, but when I create a variable using conditional statements it overwrites the variable that I previously created.
Example:
data a;
set Pathway.Tang_pathwayfinal;
if ER=1 and HER2_best=2 then subtype=1;/*luminal A*/
else if ER=1 and HER2_best=1 then subtype=2;/*luminal B*/
else if ER=2 and PR=2 and HER2_best=2 then subtype=3;/*Triple Negative*/
else if ER=2 and PR=2 and HER2_best=1 then subtype=4;/*Her2 Overexpression*/
else subtype=.;
run;
data a;
set Pathway.Tang_pathwayfinal;
if ER=1 then ERnew=1;
else if ER=2 then ERnew=2;
else ERnew=.;
run;
In this example the ERnew variable will overwrite the subtype variable in my dataset and I am not sure why. It seems that I am restricted to creating only one variable. I am using SAS v. 9.4.
Please help, Thank you!
Put all the code into one data step.
data a; set Pathway.Tang_pathwayfinal; if ER=1 and HER2_best=2 then subtype=1;/*luminal A*/ else if ER=1 and HER2_best=1 then subtype=2;/*luminal B*/ else if ER=2 and PR=2 and HER2_best=2 then subtype=3;/*Triple Negative*/ else if ER=2 and PR=2 and HER2_best=1 then subtype=4;/*Her2 Overexpression*/ else subtype=.; if ER=1 then ERnew=1; else if ER=2 then ERnew=2; else ERnew=.; run;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.