Defining new column width

Occasional Contributor
Posts: 11

Defining new column width

Hi i am creating a new variable based on an existing variable using the code below:

if recency < 6 and then status= '0-6 months';

if recency >=6 and recency <12 then status='6-12 months';

run;

However the problem i am having is that in the output table, the new column 'status' truncates parts of the data so instead of showing 6-12months, it will display 6-1 .

What statement or how can i   specify the width of the new column so it can display all the data.

Super User
Posts: 10,761

Defining new column width

You need LENGTH statement to assign the length for the new variable.

length status \$ 40 ;

..................

if recency < 6 and then status= '0-6 months';

if recency >=6 and recency <12 then status='6-12 months';

run;

Ksharp

Occasional Contributor
Posts: 15

Defining new column width

The above solution is correct...but it will allocate unnecessary memory of 40 bytes.

So instead of doing that put the second statement first and the first statement last. That will do. Always remember while writing multiple if statements to assign variable values put the if statements in decreasing length.

if recency >=6 and recency <12 then status='6-12 months';

if recency < 6 and then status= '0-6 months';

run;

Super User
Posts: 13,498

Defining new column width

I would also ask myself if I actually need a new variable for analysis. For many purposes a custom format assigned to the variable works. And if you decide to change the format you don't run into these issues.

Posts: 3,167

Re: Defining new column width

Well, I had to disagree, especially in this context.

1. Although your solution is OK in general, it usually is not the best practice. If you are worried about the waste on the length, you can always count the longest one and then put it into 'length' statement. When you do if-then flow, IMHO, the programming logic should alway go first, not the length of the arguments.

2. OP stated that the outcome showed '6-1', instead of '6-12 month'. That hinted your solution may not be sufficient. OP may have had something only 3 bytes assigned long before this two 'if' statements.

just my 2 cents,

Haikuo

Occasional Contributor
Posts: 15

Defining new column width

If in the previous case it is showing '0-6 months' then length can not be changed in between. and moreover if a variable is created by assignment the length of that variable can not be changed with a length statement.

Discussion stats
• 5 replies
• 202 views
• 0 likes
• 5 in conversation