- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Are the codes character or numeric values?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hi,
It is numeric,
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hi,
It is numeric.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hi,
Thank you so much,
It worked perfectly!
Regards,
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;