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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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