how to rename multiple variables in a dataset with new numeric variables?
The method of renaming variables has already been shown by @ed_sas_member in Message #4 in this thread.
As stated above, if you are getting character variables as a result, then something else is wrong, most likely the variables are already character variables. Please show us a PROC CONTENTS output of this data set.
If somehow PROC CONTENTS shows these variables as numeric then we need to see the EXACT code you are using to rename, and a PROC CONTENTS on the output data set.
like in the following dataset I want to replace variables cohort_11, cohort_12 and cohort_13 with numeric variable names c11,c12 and c13 respectively. how to do it?
Like this ?
data want;
set have;
rename cohort_11 = c11 cohort_12 = c12 cohort_13 = c13;
run;
yeah but by this method, I am getting character variables, but I want SAS to read the new variables as numeric variables, so how to do this?
@TANMOY05 wrote:
yeah but by this method, I am getting character variables, but I want SAS to read the new variables as numeric variables, so how to do this?
If the "renamed" variables are character then your original variables are character. You would have to use an input statement for each variable.
The picture of the data you show makes be strongly suspect that it is the result of Proc Transpose. If that is the case then likely the best approach would be to create a SINGLE numeric variable from what ever value was transposed with something like:
data need;
set have; /* the set BEFORE transpose*/
numval = input(charval,best.);
run;
Then transpose the data with the NUMVAL instead of your character value.
Hi @TANMOY05
Ok. So you can use the INPUT function:
data want;
set have;
c11 = input(cohort_11,best.);
c12 = input(cohort_12,best.);
c13 = input(cohort_13,best.);
run;
But by this method it is not renaming the variables , it is adding up variables c11, c12 and c13 with values of cohort_11, cohort_12 and cohort13 respectively. I want to rename the variables with c11, c12, c13.
Please be more precise in your diction.
Do you want to rename your variables, or replace them?
Rename
The method of renaming variables has already been shown by @ed_sas_member in Message #4 in this thread.
As stated above, if you are getting character variables as a result, then something else is wrong, most likely the variables are already character variables. Please show us a PROC CONTENTS output of this data set.
If somehow PROC CONTENTS shows these variables as numeric then we need to see the EXACT code you are using to rename, and a PROC CONTENTS on the output data set.
Yeah, after using proc contents I found out that the variables were already numeric. so now I renamed them as character variables as I needed them as character variables as later in my code I needed to merge datasets. The problem is solved now
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.