DATA Step, Macro, Functions and more

Why won't character resizing work?

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 10
Accepted Solution

Why won't character resizing work?

I have a data set that summarizes several hundred candidate models and one field is "Vars_in_model" and another is "Variable."

 

Upon declaration, I do data out Data; length Variable $27; length Variables_in_model $800; run;

 

That works, but when the data set is updated with the component macros, somehow the "Variable" gets set to size 16 and variables_in_model get set to $57. It is clearly setting it to the shortest. I have unchecked the options in SAS Eg, and re checked and re run and get the same result.

What is odd to me is I even take the output data set and try to resize with the same statements and it still stays the same size. I am doing a work around where I start with the largest first so it gets set with the largest, but this seems odd to me. Any suggestions?


Accepted Solutions
Solution
‎07-10-2017 09:54 AM
Super User
Posts: 7,866

Re: Why won't character resizing work?

Posted in reply to stratozyck

When you use more than one dataset in a set statement, and the datasets contain common variables, the attributes are set from the first dataset named in the set that contains a given variable. If subsequent datasets contain the same variable with a bigger length, the values from that are truncated, and you get a WARNING about that in the log.

Make sure that a certain variable is always created with the same length in all your datasets. This gives you a clean log and avoids confusion.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers

View solution in original post


All Replies
Solution
‎07-10-2017 09:54 AM
Super User
Posts: 7,866

Re: Why won't character resizing work?

Posted in reply to stratozyck

When you use more than one dataset in a set statement, and the datasets contain common variables, the attributes are set from the first dataset named in the set that contains a given variable. If subsequent datasets contain the same variable with a bigger length, the values from that are truncated, and you get a WARNING about that in the log.

Make sure that a certain variable is always created with the same length in all your datasets. This gives you a clean log and avoids confusion.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Super User
Posts: 7,078

Re: Why won't character resizing work?

Posted in reply to stratozyck

You need to show exactly what you did as SAS will set the length for the variable at the first point it can.

So if the LENGTH statement comes after a SET statement for a dataset that already has that variable the LENGTH statement is ignored.

Also watch out for FORMATs that might be attached to character variables.  It is best to just remove any formats that might have gotten accidentally attached to character variables.

 

Here is an example for how to create a new dataset with changed variable lengths.

data want ;
  length Variable $27 Variables_in_model $800; 
  set have ;
  format variable variables_in_model ;
run;
Occasional Contributor
Posts: 10

Re: Why won't character resizing work?

Posted in reply to stratozyck

It is fixed by changing the order of the multiple sets. I don't know why that works because both data sets had the same formatting.

Super User
Super User
Posts: 7,078

Re: Why won't character resizing work?

Posted in reply to stratozyck

stratozyck wrote:

It is fixed by changing the order of the multiple sets. I don't know why that works because both data sets had the same formatting.


By definition the datasets did not have the same structure if changing the order of the datasets in a SET statement made a difference.

Note that you cannot count on being able to fix this type of issue by just changing the dataset order. What if there is more than one variable with different lengths?  You could have a situation where any order results in one of the variables being truncated.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 79 views
  • 1 like
  • 3 in conversation