BookmarkSubscribeRSS Feed
raivester
Quartz | Level 8

Hello,

 

I am trying to generate a variable (businessnm) that takes on the values of an existing raw variable (Name) when another variable (opartytyp) has a value of "Business". Prior to this attempt, I rename a raw data variable (Type_of_Plaintiff_Resondent) to be "opartytyp". 

 

When I do this, I get the following warning: "WARNING: Variable opartytyp already exists on file WORK.CASES_1." And the "businessnm" variable is not generated. Thoughts?

 

data cases_1;
  set cases;

  	/*Rename the variables that don't need any editing*/
	rename Type_of_Plaintiff_Respondent = opartytyp;

        /*Generate a new variable (businessnm) that takes on values of the raw "Name" variable when the opartytyp variable has a value of "Business" */
	if opartytyp="Business" then businessnm=Name;
run;
7 REPLIES 7
PaigeMiller
Diamond | Level 26

Your description of the problem in words does not include a renaming step, so don't put one in. Your description of the problem in words does include an IF condition, you do need that.

 

Renaming not needed here. Take it out of your data step and run it again.

 

--
Paige Miller
raivester
Quartz | Level 8

Thank you. That worked. I moved the rename statement below the section where I generate the businessnm variable. However, I am wondering why SAS doesn't let me do the rename first . . . . 

PaigeMiller
Diamond | Level 26

What is the need to rename here? This isn't obvious from your word description of the problem. 

--
Paige Miller
raivester
Quartz | Level 8
We need to have standardized names across multiple data sets; therefore, the final variable names of this data set must follow the established convention.
FreelanceReinh
Jade | Level 19

Hello @raivester,


@raivester wrote:

(...)

Prior to this attempt, I rename a raw data variable ...

If you want the renaming to happen prior to the IF-THEN statement referring to the renamed variable (as it should), you must apply a RENAME= dataset option to the dataset read by the SET statement:

set cases(rename=(Type_of_Plaintiff_Respondent = opartytyp));

The effect of the (declarative) RENAME statement comes too late: An uninitialized* variable opartytyp is used in the IF-THEN statement and eventually replaced by the renamed variable Type_of_Plaintiff_Respondent and the name conflict causes the warning "Variable opartytyp already exists ...".

 

*assuming that opartytyp is not contained in dataset CASES -- otherwise it is the variable read from CASES, but then you cannot rename the other variable with the dataset option to this same name

Tom
Super User Tom
Super User

Don't put RENAME statements in the middle of a data step.  That is just confusing.  Put them as the LAST statement since they impact the name that is written to the output dataset(s).

data cases_1;
  set cases;
*-----------------------------------------------------------------------------;
* Generate a new variable (businessnm) that takes on values of the NAME ;
* variable when the opartytyp variable has a value of "Business" ;
*-----------------------------------------------------------------------------; 
  if Type_of_Plaintiff_Respondent="Business" then businessnm=Name;

* Rename the variables that do not need any editing ;
  rename Type_of_Plaintiff_Respondent = opartytyp;

run;

Or use the RENAME= dataset option so the name is changed BEFORE it comes into the data step.

resetline;
data cases_1;
  set cases(rename=(Type_of_Plaintiff_Respondent = opartytyp));

*-----------------------------------------------------------------------------;
* Generate a new variable (businessnm) that takes on values of the NAME ;
* variable when the opartytyp variable has a value of "Business" ;
*-----------------------------------------------------------------------------; 
  if opartytyp="Business" then businessnm=Name;

run;

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!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 7 replies
  • 1024 views
  • 0 likes
  • 4 in conversation