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.
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;
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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.