DATA Step, Macro, Functions and more

How to avoid values getting truncated when creating a new calculated column?

Accepted Solution Solved
Reply
New Contributor
Posts: 3
Accepted Solution

How to avoid values getting truncated when creating a new calculated column?

I am self learning SAS and am stuck in a very basic Issue.

Kindly refer to the code:

data work.empsal1;
set work.empsal;
if salary < 20000 then PAYBAND = "LOW" ;
ELSE IF 20000 <= SALARY < 30000 THEN PAYBAND = "MEDIUM";
ELSE PAYBAND = "HIGH";
RUN;

The values in the PAYBAND column is appearing as 'LOW', 'MED', 'HIG'

How to fix It.

It is very basic issue but as I have no direct tutor I cannot solve it. Plz help.


Accepted Solutions
Solution
‎12-07-2017 12:06 AM
Respected Advisor
Posts: 2,658

Re: How to avoid values getting truncated when creating a new calculated column?

after the SET statement, you need a LENGTH statement to specify the maximum length of the variable. If you don't it sets the length by the length of the first time the variable is assigned a value.

 

length payband $ 6;
--
Paige Miller

View solution in original post


All Replies
Solution
‎12-07-2017 12:06 AM
Respected Advisor
Posts: 2,658

Re: How to avoid values getting truncated when creating a new calculated column?

after the SET statement, you need a LENGTH statement to specify the maximum length of the variable. If you don't it sets the length by the length of the first time the variable is assigned a value.

 

length payband $ 6;
--
Paige Miller
Super User
Posts: 22,857

Re: How to avoid values getting truncated when creating a new calculated column?


avijit_p wrote:

 

It is very basic issue but as I have no direct tutor I cannot solve it. Plz help.


 People get direct tutors?

Super User
Posts: 13,066

Re: How to avoid values getting truncated when creating a new calculated column?

A very important part of SAS is the Format of variable. The format displays desired text and for almost all of the analysis procedures will use the formatted values for categories. The very fun part is that you can create formats as needed. That will allow you to create multiple displays or analysis from the same variable without having to create multiple variables as long as you only need one category set per procedure for a given variable.

Consider:

 

 

proc format library=work;
value payband
low   - < 20000 = 'LOW'
20000 - < 30000 = 'MEDIUM'
30000 - HIGH   = 'HIGH'
;
value alpayband
0  - < 20000 = 'LOW'
20000 - < 30000 = 'MEDIUM'
30000 - < 50000 = 'Higher'
50000 - High = 'HIGHEST'
;

run;

proc freq data=work.empsal;
   tables salary;
   format salary payband.;
run;

proc freq data=work.empsal;
   tables salary;
   format salary altpayband.;
run;
New Contributor
Posts: 3

Re: How to avoid values getting truncated when creating a new calculated column?

Thanks for the support.

Super User
Posts: 22,857

Re: How to avoid values getting truncated when creating a new calculated column?

The first SAS e-course is free and covers topics like this. There's also 

video.sas.com -> How To>  Analytics U

http://video.sas.com/#category/videos/sas-analytics-u

 

Lots of videos that cover these topics.

New Contributor
Posts: 3

Re: How to avoid values getting truncated when creating a new calculated column?

Thanks a lot.

The videos would of a great help.

☑ This topic is solved.

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

Discussion stats
  • 6 replies
  • 114 views
  • 1 like
  • 4 in conversation