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.
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
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;
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.
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
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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.