Help using Base SAS procedures

Defining new column width

Reply
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,018

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: 11,343

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.

Respected Advisor
Posts: 3,156

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.

Ask a Question
Discussion stats
  • 5 replies
  • 170 views
  • 0 likes
  • 5 in conversation