DATA Step, Macro, Functions and more

Creating sas datafile where variables are named in another file?

Reply
Learner
Posts: 1

Creating sas datafile where variables are named in another file?

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.

 

Super User
Posts: 6,543

Re: Creating sas datafile where variables are named in another file?

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;

Super User
Posts: 22,873

Re: Creating sas datafile where variables are named in another file?

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.

 


 

Ask a Question
Discussion stats
  • 2 replies
  • 127 views
  • 5 likes
  • 3 in conversation