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
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.
Are the codes character or numeric values?
Hi,
It is numeric,
Thanks
What is the type of indus_code?
If you had provided your data in a data step with datalines, I would not have to ask this question.
Hi,
It is numeric.
Thanks
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.
Hi,
Thank you so much,
It worked perfectly!
Regards,
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;
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 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.