SAS Viya Tutorial: Import a Headerless Text File and Rename Columns Dynamically
Simon 42 Aufrufe
Schwierigkeitsgrad
Débutant
Veröffentlicht am :
Expertenrat
Michael
While dynamically renaming variables via PROC DATASETS is highly efficient since it updates metadata without rewriting the underlying data, relying on PROC IMPORT can sometimes lead to costly data type misinterpretations. For robust, production-grade data pipelines in SAS Viya, prioritize writing a custom DATA step with an INFILE statement from the start to guarantee absolute control over formats, lengths, and memory allocation.
How can you efficiently apply your own column names without manually renaming each variable, especially if the file contains dozens of them? Here are two approaches: the quick method for a few variables, and complete automation for large files.
FILENAME LOC DISK '/workspace/workbench/myorg/data/home/olddatatoimport.txt';
PROC IMPORT DATAFILE=LOC
DBMS=DLM
OUT=WORK.NEWDATA
REPLACE;
DELIMITER=','; /* ou '09'x pour tabulation */
GETNAMES=NO; /* Crucial : évite de perdre la 1ère ligne de données */
RUN;
1
FILENAME LOC DISK '/workspace/workbench/myorg/data/home/olddatatoimport.txt';
2
3
PROC IMPORT DATAFILE=LOC
4
DBMS=DLM
5
OUT=WORK.NEWDATA
6
REPLACE;
7
DELIMITER=','; /* ou '09'x pour tabulation */
8
GETNAMES=NO; /* Crucial : évite de perdre la 1ère ligne de données */
9
RUN;
Step 2: Manual Renaming
Use PROC DATASETS to modify the names without re-reading all the data (very fast even on large volumes).
proc datasets library=WORK nolist;
modify NEWDATA;
rename
VAR1 = State
VAR2 = City
VAR3 = Population;
quit;
1
PROC DATASETS library=WORK nolist;
2
modify NEWDATA;
3
rename
4
VAR1 = State
5
VAR2 = City
6
VAR3 = Population;
7
QUIT;
2. The Automated Method (Dynamic Renaming)
If you have 50 or 100 columns listed in an external document, the manual method is tedious and prone to errors. Here’s how to automate the process by dynamically generating the RENAME statement.
The Concept
Import the data file with generic names (VAR1, VAR2...).
Phase A: Create the names table
Copy your list of columns from Word and paste it into the code below:
/* Table contenant vos noms de colonnes dans l'ordre du fichier */
data column_names;
length new_name $32;
input new_name;
datalines;
State
City
ZipCode
Population
Revenue
/* ... collez votre liste ici ... */
;
run;
1
/* Table contenant vos noms de colonnes dans l'ordre du fichier */
2
DATA column_names;
3
LENGTH new_name $32;
4
INPUT new_name;
5
DATALINES;
6
State
7
City
8
ZipCode
9
Population
10
Revenue
11
/* ... collez votre liste ici ... */
12
;
13
RUN;
Phase B: Generate the renaming script
This code loops through the list of names and builds a character string of the type: VAR1=State VAR2=City ...
data _null_;
set column_names end=last;
length rename_list $32000;
retain rename_list;
/* On concatène : "VAR" + numéro de ligne = "Nouveau Nom" */
/* cats('VAR', _n_) génère VAR1, VAR2, etc. basée sur le numéro de ligne */
rename_list = catx(' ', rename_list, catx('=', cats('VAR',_n_), new_name));
/* À la fin, on stocke tout dans une variable macro &PAIRES_RENAME */
if last then call symputx('PAIRES_RENAME', rename_list);
run;
/* Vérification dans le journal */
%put &PAIRES_RENAME;
1
DATA _null_;
2
SET column_names END=last;
3
4
LENGTH rename_list $32000;
5
retain rename_list;
6
7
/* On concatène : "VAR" + numéro de ligne = "Nouveau Nom" */
8
/* cats('VAR', _n_) génère VAR1, VAR2, etc. basée sur le numéro de ligne */
If your file is complex (specific dates, particular numeric formats), PROC IMPORT can sometimes guess the data types incorrectly. In this case, the most robust method is to write a classic DATA step.
data work.newdata;
infile '/workspace/workbench/myorg/data/home/olddatatoimport.txt'
dsd /* Gère les délimiteurs et les valeurs manquantes */
dlm=',' /* Délimiteur */
truncover; /* Évite les erreurs si une ligne est trop courte */
/* Définissez vos noms et types ici */
input
State :$2.
City :$50.
Population :best.
Revenue :comma10.;
run;
For a large number of columns: Automation via Macro Variable.
For precise control over types: DATA Step with INPUT.
Wichtiger Haftungsausschluss
Die auf WeAreCAS.eu bereitgestellten Codes und Beispiele dienen Lehrzwecken. Es ist zwingend erforderlich, sie nicht blind in Ihre Produktionsumgebungen zu kopieren. Der beste Ansatz besteht darin, die Logik zu verstehen, bevor sie angewendet wird. Wir empfehlen dringend, diese Skripte in einer Testumgebung (Sandbox/Dev) zu testen. WeAreCAS übernimmt keine Verantwortung für mögliche Auswirkungen oder Datenverluste auf Ihren Systemen.
SAS und alle anderen Produkt- oder Dienstleistungsnamen von SAS Institute Inc. sind eingetragene Marken oder Marken von SAS Institute Inc. in den USA und anderen Ländern. ® zeigt die Registrierung in den USA an. WeAreCAS ist eine unabhängige Community-Site und nicht mit SAS Institute Inc. verbunden.
Diese Website verwendet technische und analytische Cookies, um Ihre Erfahrung zu verbessern.
Mehr erfahren.