BookmarkSubscribeRSS Feed
Kirito1
Quartz | Level 8

For example: Data should contain the following variables.

CUSTOMER_ID, AGE, MOB_NO. 

but the variable names in the csv are like 

cusTomer No, aGe, Number

All the variable names are always differing due the the guy delivering me the data.

Now what I want is regardless of what data I receive I should always discard the first row and assign my own variables without even considering what variables were there in the CSV.

 

This is helpful when we know that data is always same only the variable names differ so we need not to go and change variable names in excel and then save it and then import it, rather we just import the file with the pre-defined variable names that we always want.

9 REPLIES 9
Kurt_Bremser
Super User

That's why you never use PROC IMPORT for text files, but write the data step yourself.

Copy the data step code created by PROC IMPORT from the log, and change the variable names.

Kirito1
Quartz | Level 8
If the data is in .xls format then what we can do.
Kirito1
Quartz | Level 8
Can we use infile for that without considering what file format is data in?
andreas_lds
Jade | Level 19

@Kirito1 wrote:
Can we use infile for that without considering what file format is data in?

No, infile and input statement must match the structure of the file.

Kirito1
Quartz | Level 8

I just want to change variable names while importing a csv without doing the following

RENAME = (old1=new1 old2=new2 .... oldk=newk);

 is there a way through which I could change variable names without considering what variables are there in the csv file.

Kurt_Bremser
Super User

@Kirito1 wrote:

I just want to change variable names while importing a csv without doing the following

RENAME = (old1=new1 old2=new2 .... oldk=newk);

 is there a way through which I could change variable names without considering what variables are there in the csv file.


There is absolutely NO need to RENAME variables when reading a csv file. A csv file is read with INFILE  and INPUT in a DATA step, and the variable names are defined in the INPUT statement.

andreas_lds
Jade | Level 19

@Kirito1 wrote:

I just want to change variable names while importing a csv without doing the following

RENAME = (old1=new1 old2=new2 .... oldk=newk);

 is there a way through which I could change variable names without considering what variables are there in the csv file.


If the position of the variables are constant and only the names are set randomly, you could ignore the names (getnames=no) during proc import and use the rename option.

Kurt_Bremser
Super User

INFILE and INPUT in a DATA step can be used for text files; Excel files need PROC IMPORT or LIBNAME EXCEL/XLSX.

The results of reading Excel files directly are inconsistent at best; it is always better to save the data to a text file and read that with a DATA step.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 9 replies
  • 1181 views
  • 0 likes
  • 3 in conversation