BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
amanjot_42
Fluorite | Level 6

Hi all,

 

I am trying to extract the first two digits of various industry codes. Generally, the codes are in five digits, but there are several codes which are either single, two or four digits. In these cases, I simply want to extract the first two digits. Can you please help me with the codes?!

 

Sample:

 

Firm ID  Indus_Code 2-digits(desired)

11                8                8

12               77              77

13          15345             15

14            1678             16

 

Regards,

Amanjot

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Then try this:

data have;
input ID :$2. indus_code;
datalines;
11 8
12 77
13 15345
14 1678
;

data want;
set have;
ind_code_2 = input(substr(put(indus_code,5.-l),1,2),2.);
run;

But I would prefer to store codes as character. Character data would be left-aligned anyway, and you only need the substr() for that. And you don't do calculations with codes.

View solution in original post

8 REPLIES 8
PeterClemmensen
Tourmaline | Level 20

Are the codes character or numeric values?

amanjot_42
Fluorite | Level 6

Hi,

It is numeric,

Thanks

amanjot_42
Fluorite | Level 6

Hi,

It is numeric.

Thanks

Kurt_Bremser
Super User

Then try this:

data have;
input ID :$2. indus_code;
datalines;
11 8
12 77
13 15345
14 1678
;

data want;
set have;
ind_code_2 = input(substr(put(indus_code,5.-l),1,2),2.);
run;

But I would prefer to store codes as character. Character data would be left-aligned anyway, and you only need the substr() for that. And you don't do calculations with codes.

amanjot_42
Fluorite | Level 6

Hi,

Thank you so much,

It worked perfectly!

 

Regards,

PeterClemmensen
Tourmaline | Level 20

One way

 

data have;
input FirmID Indus_Code;
datalines;
11 8
12 77
13 15345
14 1678
;

data want;
   set have;
   firstTwo = input(substr(put(Indus_Code, 8. -l), 1, 2), 8.);
run;
Ksharp
Super User

Just for fun.

 

data have;
input ID :$2. indus_code;
datalines;
11 8
12 77
13 15345
14 1678
;
data want;
 set have;
 power=ceil(log10(indus_code));
 power=ifn(power-2<=0,0,power-2);
 ind_code_2=int(indus_code/10**power);
 run;

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 8 replies
  • 10226 views
  • 0 likes
  • 4 in conversation