Help using Base SAS procedures

Adding Variable Names to SAS Dataset

Accepted Solution Solved
Reply
Highlighted
Occasional Contributor
Posts: 6
Accepted Solution

Adding Variable Names to SAS Dataset

Hello Community!

 

I am trying to replace SAS variable names for a dataset that has been loaded to the server. For instance, SAS dataset (Var1, Var2, Var3, Var4, etc.). Need to change Var"" to: CustomerNo., CustName, Product, Sale, etc.. No need to do import because the table is already available in on the server.

 "abc.filename"

 

This is what I have. Where have I gone wrong?

Actual

Var1          Var2          Var3          Var4

123            Ted           Knife          $25

124            Bill            Watch        $55

 

Need

CustomerNo          CustName            Product          Sale

123                         Ted                       Knife               $25

124                         Bill                        Watch             $55

 

data FactSetNames;

     input FileNames $30.;

     datalines;

 

CustomerNo

CustName

Product

Sale 

     ;

run;

 

%macro FactSetloop;

     %do i = 1 %to 255;

           data _NULL_;

                set FactSetNames;

                if _N_ = &i then call symputx("FileName",FileNames,'G');

           run;

 

          Data step?

 

           run;

     %end;

%mend;

%FactSetloop


Accepted Solutions
Solution
a week ago
PROC Star
Posts: 7,800

Re: Adding Variable Names to SAS Dataset

If you already have the factsetnames file then there isn't anything tedious about the code at all. Here is the same code, but which uses the already created factsetnames file. It would accomplish the renaming for all of the variables in the factsetnames file:

data _null_;
  length forexec $255;
  set FactSetNames end=eof;
  if _n_ eq 1 then call execute ("data Need; set Actual;");
  forexec=catt("rename var",_n_,"=",FileNames,";");
  call execute(forexec);
  if eof then call execute ("run;");
run;

Art, CEO, AnalystFinder.com

 

 

View solution in original post


All Replies
Super User
Super User
Posts: 8,634

Re: Adding Variable Names to SAS Dataset

Why are they being loaded in as var1 var2 in the first place?  Fix the broken part, don't try to repair it after the fact.  I suspect its likely your importing a spreadsheet.  Bin that, import a csv and write a datastep to do the process.  That way you can read in the data you want, set labels, names and lengths etc. at load time.

PROC Star
Posts: 7,800

Re: Adding Variable Names to SAS Dataset

Here is one way:

data _null_;
  length forexec $255;
  input FileNames $30.;
  if _n_ eq 1 then call execute ("data Need; set Actual;");
  forexec=catt("rename var",_n_,"=",FileNames,";");
  call execute(forexec);
  if _n_ eq 4 then call execute ("run;");
  datalines;
CustomerNo
CustName
Product
Sale 
;

Art, CEO, AnalystFinder.com

 

Super User
Posts: 6,004

Re: Adding Variable Names to SAS Dataset

Where did you go wrong?  By starting to write a program.  First, you should have read related pieces of the manual, so you would have an idea of what the program should look like.  Some topics:

 

The RENAME statement

The RENAME= option on a SET statement

The MODIFY statement within PROC DATASETS

 

Once you have some idea of what the program should look like, you can begin the programming.  One related hint:  you would need two variables in a SAS data set, for example, OLDNAME and NEWNAME.  They should both be character, 32 characters long (maximum length for a variable name in SAS).  A single variable (FileNames) is not enough.  So when OLDNAME is Var1, NEWNAME would be CustomerNo.

Occasional Contributor
Posts: 6

Re: Adding Variable Names to SAS Dataset

The dataset was loaded by another person. I only have a seperate variable name key. Don't have access to the original file
Occasional Contributor
Posts: 6

Re: Adding Variable Names to SAS Dataset

Thank you for your solution. The problem is, I forgot to state, is that there are 150 variable names which doing each one is tedious and time consuming.
Solution
a week ago
PROC Star
Posts: 7,800

Re: Adding Variable Names to SAS Dataset

If you already have the factsetnames file then there isn't anything tedious about the code at all. Here is the same code, but which uses the already created factsetnames file. It would accomplish the renaming for all of the variables in the factsetnames file:

data _null_;
  length forexec $255;
  set FactSetNames end=eof;
  if _n_ eq 1 then call execute ("data Need; set Actual;");
  forexec=catt("rename var",_n_,"=",FileNames,";");
  call execute(forexec);
  if eof then call execute ("run;");
run;

Art, CEO, AnalystFinder.com

 

 

Occasional Contributor
Posts: 6

Re: Adding Variable Names to SAS Dataset

Code worked marvelously! Thank you.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 237 views
  • 1 like
  • 4 in conversation