BookmarkSubscribeRSS Feed
sas21
Calcite | Level 5

I have a data set "compare_cohort" with the following variables....

 

ID     COUNTRY      BRTHDT  DTHDTRACE ......etc it has a lot of variables

 

and I want to make every variable name end in _new, so the new variable name would look like;

ID_NEW                COUNTRY_NEW                BRTHDT_NEW                  DTHDT_NEW                 RACE_NEW             ETC....

 

Everything I am trying is adding it to the variable values and not the variable names themselves.  Any help is appreciated!   Thank you so much!

11 REPLIES 11
Reeza
Super User

Here's a quick way using the dictionary tables to get the list of the variable names, and create the new name using the CATT() function. 

PROC DATASETS will update the data with the new variable names.

 

*create sample data set;
data class;
set sashelp.class;
run;


proc sql noprint;
select catx("=", name, catt(name, "_NEW")) /*creates oldvariable = newvariable for rename statement*/
into :rename_list
separated by " "
from sashelp.vcolumn
where libname='WORK' /*replace with your library, or WORK*/
and memname='CLASS' /*replace with data set name - note that it must be upper case, same as libname*/
;
quit;

*check the variable rename list in the log;
%put &rename_list;

*rename the variables without rewriting the full data set;
proc datasets library=work nodetails nolist;
modify class;
rename &rename_list;
run; quit;

*check output;
proc print data=class noobs;
run;

@sas21 wrote:

I have a data set "compare_cohort" with the following variables....

 

ID     COUNTRY      BRTHDT  DTHDTRACE ......etc it has a lot of variables

 

and I want to make every variable name end in _new, so the new variable name would look like;

ID_NEW                COUNTRY_NEW                BRTHDT_NEW                  DTHDT_NEW                 RACE_NEW             ETC....

 

Everything I am trying is adding it to the variable values and not the variable names themselves.  Any help is appreciated!   Thank you so much!


 

PaigeMiller
Diamond | Level 26

So do you also have a dataset where you don't want the _NEW on these exact same variables?

 

If so, then I think there's a better way to go. Just vertically join the data sets (SET command in a data step) and create a variable to indicate where or not the data is the NEW data or the "old" data, for lack of a better term. No renaming needed. 

 

Analysis is usually much easier with the data arranged as described above.

 

data combined;
    set old_data_set_name new_data_set_name indsname=indsname;
    dsname=indsname;
run;

 

How easy is that?

 

But please describe the problem further, why are you doing this and what will you do after you create this data set? Context is everything and you have given us no context; and in many contexts arranging the data the way you describe is not the easiest thing to do, and it can make the next steps harder.

--
Paige Miller
sas21
Calcite | Level 5

I have two of the same datasets that contain the exact same variable names, but they are different in what they contain.  I need to compare age, race and other demographic variables.  I wanted to change the the variable names in one of the datasets so I could tell the two apart, and I am not sure if the variables need to be different when using PROC COMPARE or if it will throw an error.

Patrick
Opal | Level 21

@sas21  It's actually a bit less coding if the variable names are the same. If that's not the case then you need to use the VAR and WITH statements to instruct Proc Compare to use which variables from the one datasets for comparison with which variable from the other dataset. It's all documented COMPARE Procedure examples included.

sas21
Calcite | Level 5

PROC COMPAREBYIDVARWITHWITH

Compare variables of different names

 

But, according to this example, it says the variables need to have different names.  And as of now, the two datasets I want to compare have the exact same variable names, which is why I thought it would be better to change the variable names in one of them.  Or am I misinterpreting something?

Reeza
Super User

@sas21 wrote:

PROC COMPAREBYIDVARWITHWITH

Compare variables of different names

 

But, according to this example, it says the variables need to have different names.  And as of now, the two datasets I want to compare have the exact same variable names, which is why I thought it would be better to change the variable names in one of them.  Or am I misinterpreting something?


You can compare two data sets by ID with the same variable names without having to rename. 

 

Patrick
Opal | Level 21

@sas21 Yes, you must be misinterpreting something.

- The VAR statement allows you to define which variables to use for the comparison. If you don't use it then all variables in the tables will get used.

- The WITH statement allows you to compare variables with different names. You can use the VAR statement for a list of variables in the base table and WITH for the list of variables in the compare table. Proc compare will then just use the names positionally and compare the first variable from VAR with the first variable from WITH, 2nd with 2nd, 3rd with 3rd etc.

 

Below working sample code for a case where the variables have the same name in both source tables. Just run it and see what happens.

data work.class1 work.class2;
  set sashelp.class;
  output work.class1;
  if _n_=5 then
    do;
      age=9;
      height=52.7;
    end;
  if _n_=9 then
    do;
      sex='x';
    end;
  output work.class2;
run;

proc compare base=work.class1 compare=work.class2;
/* var age height;*/
run;
ballardw
Super User

@sas21 wrote:

I have two of the same datasets that contain the exact same variable names, but they are different in what they contain.  I need to compare age, race and other demographic variables.  I wanted to change the the variable names in one of the datasets so I could tell the two apart, and I am not sure if the variables need to be different when using PROC COMPARE or if it will throw an error.


What do you mean by "compare"? SAS has a procedure named Compare that is used to examine entire data sets.

If you actually mean something more like "examine differences in results of statistical tests" or distributions of value you are way better off providing an additional variable to indicate the new/old data set and using that as class or grouping variable.

 

An example you can run where I compare the height and weight of students by SEX in a data set using a TTest:

proc ttest data=sashelp.class;
   class sex;
   var height weight;
run;

Or using Sex to see the distribution of another variable such as AGE:

Proc freq data=sashelp.class;
   tables sex*age;
run;

If you do something like the following then a New data set with all the observations will be created and a variable SOURCE will have the library and data set name of each of the source data sets and can be used as a grouping variable for any analysis.

data new;
   set dataset1   dataset2 indsname=sourcename;
   source=sourcename;
run;

Caution: if any of the variables with the same name are of different types you will get an error and indicates your "comparison" would be problematic at best involving those variables. If the lengths of a character variable differ in the two sets, again a problem with "comparison", you will see a note about possible truncation and loss of data.

 

ballardw
Super User

Have you verified that all of your existing variables are short enough names that adding 4 characters will not violate SAS variable naming rules? If you have a variable that already has 32 characters in the name you cannot add any. So what should be done in this case. Or if any name longer than 28 you can only add to a total of 32. So are you going to want variables that end in _NE or _N ?

sas21
Calcite | Level 5
Thank you for the reminder! I just double checked and I should be good!
PaigeMiller
Diamond | Level 26

In this example, the variable in both data sets has the name YEAR

https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.4/proc/p1k00d45g03uv8n1bfx3d20breg6.htm 

 

This is a great example of why you should tell us, in your first post on a subject, what you are doing and why you are doing it, rather than focusing strictly on coding (how to add suffixes to a variable name).

--
Paige Miller

Ready to join fellow brilliant minds for the SAS Hackathon?

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!
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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 11 replies
  • 1177 views
  • 9 likes
  • 5 in conversation