Help using Base SAS procedures

to get left 3 digits of numeric value

Reply
New User
Posts: 1

to get left 3 digits of numeric value

Hi,

 

In a column, the employee code having 6 digits is mentioned . If I want to see the first 3 digit of the employee code in a column, please suggest the sas code for this.

Employeecodefirst 3 digits
174078174
121764121
190333190
214078214
Super User
Super User
Posts: 7,942

Re: to get left 3 digits of numeric value

Posted in reply to mprasannna

Hi,

 

Assuming that the id is text:

three_chars=substr(left(id),1,3);

 

If number then:

three_chars=substr(put(id,6.),1,3);

Regular Contributor
Posts: 216

Re: to get left 3 digits of numeric value

Posted in reply to mprasannna

if the employee code is a number, then you would need to convert it to a character first, then use Character functions to get the first three digits.

 

Data _null_;
 employee_code = 174078; /* Numeric */
 first_3_digits = INPUT(SUBSTR(PUT(employee_code,6.),1,3),3.); /* Num -> Char -> Num */
 put _all_;
run;

 

Frequent Contributor
Posts: 95

Re: to get left 3 digits of numeric value

Posted in reply to mprasannna

Try this code;

data one; infile cards;
input @1 empid;
cards;
174078
121764
190333
214078
;
data; set; format x 3.;
x=trunc((empid/1000),3);
proc print; run;

 

Jim

Valued Guide
Posts: 765

Re: to get left 3 digits of numeric value

Posted in reply to mprasannna

Hi.  If EMPID is numeric ...

 

data one;
input empid @@;
datalines;
174078 121764 190333 214078
;

 

data two;
set one;
three = input(cat(empid),3.);
run;

 

data set TWO ...

Obs empid  three

1   174078  174
2   121764  121
3   190333  190
4   214078  214

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