My code is:
data want;
length name $ 300;
format name $300.;
set have (obs=0);
run;
This code takes in a data set "have" (which has a character variable "name" in it) and I want it instead to output only the column names of "have", with the new "name" variable having length 300. However, the output changes the column position of "name" and places it as the first column. How do I retain its column position.
data have;
set sashelp.class;
run;
proc sql;
alter table have
modify name char(400),sex char(200);
quit;
Hi, this changes its length but it doesn't change its format so my data still truncates.
@yellowyellowred wrote:
Hi, this changes its length but it doesn't change its format so my data still truncates.
The data is not truncated, only the number of chars you see is still limited. You can change that by using proc dataset and modify. Have a look at the docs for details.
data have;
set sashelp.class;
run;
proc sql;
alter table have
modify name char(400) format=$400.,sex char(200) format=$200.;
quit;
SAS creates variables in a data step during the compilation phase using the attributes from the first occurrence it encounters the variable.
If you want to maintain the variable order then you could do something like:
data want;
length name $ 300;
set have (obs=0 keep=<var on pos 1> -- <var before name>);
length name $ 300;
set have (obs=0 keep=<var after name>--<last var>);
run;
Same applies for any combination of tables: The variable definition will come from the first source table this variable exist in.
Added later: Use the coding approach @Ksharp proposes. It's cleaner.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Select SAS Training centers are offering in-person courses. View upcoming courses for: