Published on :
Général SASHELP

Sans titre

This code is also available in: Deutsch Español Français
Awaiting validation
This script illustrates a common problem: modifying the length of a variable often changes its position in the table (it ends up at the end). The proposed solution uses metadata (SASHELP.VCOLUMN) to capture the initial order of variables, then uses the RETAIN statement in a DATA step combined with a LENGTH statement to apply the format change while forcing the column order defined by the macro variable.
Data Analysis

Type : SASHELP


Uses the example table 'sashelp.cars' as data source and 'sashelp.vcolumn' for structure metadata.

1 Code Block
DATA STEP Data
Explanation :
Creation of a 'cars' working table (in the WORK library) from a subset of columns from 'sashelp.cars'.
Copied!
1 
2DATA cars;
3SET sashelp.cars (keep=Make Model Type Origin Drivetrain);
4RUN;
5 
2 Code Block
PROC SQL
Explanation :
Extraction of the list of variable names from the 'cars' table from the data dictionary (VCOLUMN), stored in the macro-variable ':varlist' to preserve the current order.
Copied!
1PROC SQL noprint;
2 select name into :varlist separated BY ' '
3 from sashelp.vcolumn
4 where upcase(LIBNAME) eq 'WORK' and upcase(memname) eq 'CARS';
5QUIT;
3 Code Block
DATA STEP Data
Explanation :
Reconstruction of the 'cars' table. The RETAIN statement (using the previously retrieved variable list) forces the column order. The LENGTH statement modifies the size of the 'Type' variable from 8 to 10 characters.
Copied!
1DATA cars;
2retain &varlist;
3LENGTH Type $10;
4 SET cars;
5RUN;
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.
Copyright Info : Author(s) : Nicolas DUPONT, Date : 22/04/2017