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!
data cars;
set sashelp.cars (keep=Make Model Type Origin Drivetrain);
run;
1
2
DATA cars;
3
SET sashelp.cars (keep=Make Model Type Origin Drivetrain);
4
RUN;
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!
proc sql noprint;
select name into :varlist separated by ' '
from sashelp.vcolumn
where upcase(libname) eq 'WORK' and upcase(memname) eq 'CARS';
quit;
1
PROC SQL noprint;
2
select name into :varlist separated BY' '
3
from sashelp.vcolumn
4
where upcase(LIBNAME) eq 'WORK' and upcase(memname) eq 'CARS';
5
QUIT;
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!
data cars;
retain &varlist;
length Type $10;
set cars;
run;
1
DATA cars;
2
retain &varlist;
3
LENGTH Type $10;
4
SET cars;
5
RUN;
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
SAS and all other SAS Institute Inc. product or service names are registered trademarks or trademarks of SAS Institute Inc. in the USA and other countries. ® indicates USA registration. WeAreCAS is an independent community site and is not affiliated with SAS Institute Inc.
This site uses technical and analytical cookies to improve your experience.
Read more.