DATA Step, Macro, Functions and more

Help on SAS Functions

Reply
Occasional Contributor
Posts: 19

Help on SAS Functions

Hi All,

Hope everyone doing good.

I would like to have some idea on the below situtaion which i have come accross recently. I have dataset called Hospitals which contain the hospital_name variable and it is having the data as mentioned below. I need to remove the country name in the every records. Please find the example below.

Obs   Hopspital_Name

1       INDIA - ABC HEALTHCARE

2       CHINA - EFF HEALTHCARE

3       USA  - GHI HEALTHCARE

4       UK - KLK HEALTHCARE

5       -


6       -


.

.

.

.

.

210  SPAIN-XYZ Healthcare.

Can you please help me how to trim the country name for 210 countries

Thanks,

Kannan

Super User
Super User
Posts: 7,988

Re: Help on SAS Functions

Posted in reply to KannanBaskar

Hi,

data want;

     set have;

     name_of_hospital=substr(hospital_name,index(hospital_name,"-")+1);

run;

Trusted Advisor
Posts: 1,230

Re: Help on SAS Functions

Posted in reply to KannanBaskar

data want;

     set have;

     Hopspital_Name=scan(Hopspital_Name,2,'-');

run;

N/A
Posts: 1

Re: Help on SAS Functions

Posted in reply to KannanBaskar

Despite there already being two perfectly good answers, here is a third, possibly slighlty more cryptic and possibly slightly more flexible.

data want

  set have;

  Hospital_Name=prxchange('s/^.+-\s//'1,Hospital_Name);

run;

I think prxchange was introduced in SAS9.

Occasional Contributor
Posts: 19

Re: Help on SAS Functions

Thanks George for your answers,

I have another query on the same issues. I just need to remove '-' if hospital name contain any country name. Please see the dataset below.

Obs   Hopspital_Name

1       INDIA - ABC HEALTHCARE

2       CHINA - EFF HEALTHCARE

3       USA  - GHI HEALTHCARE

4       UK - KLK HEALTHCARE

5       HAPPY - HEALTHCARE

6       WELL - HEALTHCARE

7       WEALTH - HEALTHCARE

8       FREE - HEALTHCARE

.         .

1800  AUS - HEALTHCARE

i look forwared hear from you.

Thanks in Advance,

Kannan B.

Regular Contributor
Posts: 168

Re: Help on SAS Functions

Posted in reply to KannanBaskar

you can use compress function to remove '-'.

Super User
Super User
Posts: 7,988

Re: Help on SAS Functions

Posted in reply to KannanBaskar

Hi,

To clarify, what do you want the output to be as whilst you can use the compress function as RamKumar has mentioned you also need to know if a country is in the first part.  Do you have some kind of list in place to identify all different spellings of country?  If so then:

proc sql;

     create table WANT as

     select     OBS,

                    case     when scan(HOSPITAL_NAME,2,'-') in (select distinct COUNTRY from WORK.LIST_OF_COUNTRIES) then

                                      compress(HOSPITAL_NAME,"-")

                                  else HOSPITAL_NAME end as HOSPITAL_NAME

     from       HAVE;

quit;

PROC Star
Posts: 7,492

Re: Help on SAS Functions

Posted in reply to KannanBaskar

Are you just trying to do something like?:

data have;

  format Hospital_Name $80.;

  input Hospital_Name 1-80;

  if countw(scan(Hospital_Name,2,'-')) eq 1 then

   Hospital_Name=compress(Hospital_Name,'-');

  else Hospital_Name=scan(Hospital_Name,2,'-');

  cards;

INDIA - ABC HEALTHCARE

CHINA - EFF HEALTHCARE

USA  - GHI HEALTHCARE

UK - KLK HEALTHCARE

HAPPY - HEALTHCARE

WELL - HEALTHCARE

WEALTH - HEALTHCARE

FREE - HEALTHCARE

AUS - HEALTHCARE

;

Ask a Question
Discussion stats
  • 7 replies
  • 453 views
  • 0 likes
  • 6 in conversation