BookmarkSubscribeRSS Feed
yellowyellowred
Obsidian | Level 7

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. 

5 REPLIES 5
Ksharp
Super User
data have;
 set sashelp.class;
run;


proc sql;
alter table have
modify name char(400),sex char(200);
quit;
yellowyellowred
Obsidian | Level 7

Hi, this changes its length but it doesn't change its format so my data still truncates.

andreas_lds
Jade | Level 19

@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.

Ksharp
Super User
data have;
 set sashelp.class;
run;


proc sql;
alter table have
modify name char(400) format=$400.,sex char(200)  format=$200.;
quit;
Patrick
Opal | Level 21

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.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 824 views
  • 2 likes
  • 4 in conversation