SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
liebera
Calcite | Level 5

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!

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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!


 

View solution in original post

3 REPLIES 3
Reeza
Super User

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!


 

liebera
Calcite | Level 5

Wow, what a silly mistake! Thank you so much, I am quite new to this. I appreciate your quick reply and help.

ballardw
Super User

@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;

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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
  • 3 replies
  • 4123 views
  • 0 likes
  • 3 in conversation