My data looks like this..
LIMCEE TABLET
CELINTABLET
AMOXYCLAV CAP
RECLIMET OD TAB
GLYCOMET SR TAB
REFRESH TEARS
WELL WOMEN CAPSULE
RENERVECAPSULE
MET XL TABLET
MET XL AM TABLET
SHELCAL
SHELCAL HD
SHELCAL HD12
TENCLIMET TABLET
ZITAMET PLUS TABLET
PREGAB M CAP
LOSAR H40 TABLET
LOSAR H80 TAB
PRAZOPRESS XL 25 TABLET
I want to create separate dataset for names ending with TAB and TABLET as TAB_INFO and
names ending with CAP and CAPSULES as CAP_INFO.Tried something with substr and substrn..but nothing worked
My data looks like this..
LIMCEE TABLET
CELINTABLET
AMOXYCLAV CAP
RECLIMET OD TAB
GLYCOMET SR TAB
REFRESH TEARS
WELL WOMEN CAPSULE
RENERVECAPSULE
MET XL TABLET
MET XL AM TABLET
SHELCAL
SHELCAL HD
SHELCAL HD12
TENCLIMET TABLET
ZITAMET PLUS TABLET
PREGAB M CAP
LOSAR H40 TABLET
LOSAR H80 TAB
PRAZOPRESS XL 25 TABLET
I want to create separate dataset for names ending with TAB and TABLET as TAB_INFO and
names ending with CAP and CAPSULES as CAP_INFO.Tried something with substr and substrn..but nothing worked
maybe something like this? Character Functions
data have;
input string $35.;
datalines;
LIMCEE TABLET
CELINTABLET
AMOXYCLAV CAP
RECLIMET OD TAB
GLYCOMET SR TAB
REFRESH TEARS
WELL WOMEN CAPSULE
RENERVECAPSULE
MET XL TABLET
MET XL AM TABLET
SHELCAL
SHELCAL HD
SHELCAL HD12
TENCLIMET TABLET
ZITAMET PLUS TABLET
PREGAB M CAP
LOSAR H40 TABLET
LOSAR H80 TAB
PRAZOPRESS XL 25 TABLET
;
run;
data TAB_INFO CAP_INFO OTHERS;
set have;
if substr(upcase(string),length(string)-5)='TABLET' or substr(upcase(string),length(string)-2)='TAB' then output TAB_INFO;
else if substr(upcase(string),length(string)-6)='CAPSULE' or substr(upcase(string),length(string)-2)='CAP' then output CAP_INFO;
else output OTHERS;
run;
Hey thanks !!!! Wong it worked.But can you try with substrr(right of) function??
That does use substr (right of) function. In this case the = is merely a substitute for eq and performs the right of action.
Art, CEO, AnalystFinder.com
Looks like something that it easy to handle with SCAN() function. You can use negative index to scan from the right instead of left.
data TAB_INFO CAP_INFO;
set have ;
if scan(MYVAR,-1) in ('TAB','TABLET') then output TAB_INFO;
else if scan(MYVAR,-1) in ('CAP','CAPSULES') then output CAP_INFO;
run;
Or perhaps even better you can use the SELECT statement.
data TAB_INFO CAP_INFO;
set have ;
select (scan(MYVAR,-1));
when ('TAB','TABLET') output TAB_INFO;
when ('CAP','CAPSULES') output CAP_INFO;
otherwise;
end;
run;
I think scan function is not correct in this case,if you see the data,in some records CAP/CAPSULE/TAB/TABLET are a not separately mentioned.I mean tab and cap are not separated as words.As per my knpwledge is concerned scan works on word basis.Hence here substr is perfect
@Bhargav_Movva wrote:
I think scan function is not correct in this case,if you see the data,in some records CAP/CAPSULE/TAB/TABLET are a not separately mentioned.I mean tab and cap are not separated as words.As per my knpwledge is concerned scan works on word basis.Hence here substr is perfect
I depends on what you are trying to do. Your example string will scan into words using the default delimiters. You can use other delimiters with the scan() function.
205 data _null_; 206 str='CAP/CAPSULE/TAB/TABLET'; 207 lastword=scan(str,-1); 208 put (str lastword) (=); 209 run; str=CAP/CAPSULE/TAB/TABLET lastword=TABLET
data have;
input product_name$40.;
datalines;
LIMCEE TABLET
CELINTABLET
AMOXYCLAV CAP
RECLIMET OD TAB
GLYCOMET SR TAB
REFRESH TEARS
WELL WOMEN CAPSULE
RENERVECAPSULE
MET XL TABLET
MET XL AM TABLET
SHELCAL
SHELCAL HD
SHELCAL HD12
TENCLIMET TABLET
ZITAMET PLUS TABLET
PREGAB M CAP
LOSAR H40 TABLET
LOSAR H80 TAB
PRAZOPRESS XL 25 TABLET
;
run;
data want;
set have;
if index(product_name,"TABLET") then Remark = 'INFO_TAB';
else if index(product_name,"TAB") then Remark = 'INFO_TAB';
else if index(product_name,"CAP") then Remark = 'INFO_CAP';
else if index(product_name,"CAPSULE") then Remark = 'INFO_CAP';
RUN;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.