BookmarkSubscribeRSS Feed
Fol_
Calcite | Level 5

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.

5 REPLIES 5
Ksharp
Super User

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

arnab61
Calcite | Level 5

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;

ballardw
Super User

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.

Haikuo
Onyx | Level 15

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

arnab61
Calcite | Level 5

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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

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.

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
  • 950 views
  • 0 likes
  • 5 in conversation