BookmarkSubscribeRSS Feed
amaple
Calcite | Level 5

Hi there. I am on SAS 9.4. I have two csv files, one containing data and another containing two columns, one for the variables of the first file and the second for what I am supposed to rename them to, something like this:

 

file 1 - base age yr

           some data..

           some data..

           some data..

 

file 2 - 

var_dict  var2

base       Baseline 

age         Age at first diagnosis

yr            Duration in years

 

How am I supposed to get around to renaming file1's variables to file2's? I was thinking PROC TRANSPOSE and an if loop but I can't quite seem to get my head around the logic of it? Any help is appreciated. My code so far is just importing both csv files I'm stuck from there.

 

2 REPLIES 2
Astounding
PROC Star

First step:  convert your csv files to SAS data sets.  Then we can begin the conversation.

 

Second step:  consider whether you really want to do this.  If you have a variable named age, you can refer to it as age.  If you have a variable named Age at first diagnosis, your programs will need to refer to it as:

 

'Age at first diagnosis'n

 

The program gets a bit clumsy but it can be done.

 

Third step depends on your decision in step 2.  Do you really want to change the variable names, or do you just want to assign labels to the variables?  In most cases, you will have a choice of whether a report should print the variable name vs. the variable label.

 

Fourth step:  Use the second file to write to a temporary file, containing the code that you need to implement.  For  example, the second file might be created to contain:

 

label age = 'Age at first diagnosis';

 

Or it might be designed to contain:

 

rename age = 'Age at first diagnosis'n;

 

Whichever your choice, the second file becomes the input to a DATA step, and that DATA step writes out either a set of LABEL statements or a set of RENAME statements.

 

Fifth step:  apply that file to the first data set.  The general idea:

 

data want;

set first_dataset;

%include "file holding either RENAME or LABEL statements";

run;

Reeza
Super User

Those don't look like variable names, they look like labels. 

 

Here's an example of how you can do that, with a fully worked example code that you can run. This is a dynamic approach, but if the macro variables were to become larger than 65K characters it wouldn't work. So as long as your variable lists is relatively short you should be fine. 

 

https://gist.github.com/statgeek/f18931085f6a0009185c

 


@amaple wrote:

Hi there. I am on SAS 9.4. I have two csv files, one containing data and another containing two columns, one for the variables of the first file and the second for what I am supposed to rename them to, something like this:

 

file 1 - base age yr

           some data..

           some data..

           some data..

 

file 2 - 

var_dict  var2

base       Baseline 

age         Age at first diagnosis

yr            Duration in years

 

How am I supposed to get around to renaming file1's variables to file2's? I was thinking PROC TRANSPOSE and an if loop but I can't quite seem to get my head around the logic of it? Any help is appreciated. My code so far is just importing both csv files I'm stuck from there.

 


 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

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
  • 2 replies
  • 886 views
  • 5 likes
  • 3 in conversation