keep if variable contains....

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 102
Accepted Solution

keep if variable contains....

hi, im a new user and i m not sure how to keep the variables if the contain...

for example i have ISIN: HK0000077468. i want to keep the observations if  ISIN contains HK.

could you kindly provide ur guidance?


Accepted Solutions
Solution
‎04-16-2014 01:57 PM
Contributor
Posts: 43

Re: keep if variable contains....

data newdata;

set original;

if find( ISIN,'HK','i')>0;

run;

View solution in original post


All Replies
Solution
‎04-16-2014 01:57 PM
Contributor
Posts: 43

Re: keep if variable contains....

data newdata;

set original;

if find( ISIN,'HK','i')>0;

run;

Frequent Contributor
Posts: 102

Re: keep if variable contains....

thanks for ur kind reply. 2 additional question:

1) how would u write the code just to parse the 1st 2 characters? what does 'i' mean?

2) how can write the code if i want to do HK and CN?

ie)

data newdata;

set original;

if find( ISIN,'HK','i')>0 & find(ISIN, 'CN', 'i')>0;

run;

is not working....

thank you!

Super Contributor
Posts: 1,636

Re: keep if variable contains....

using substr:

if substr(your_variable,1,2) in ('HK','CN');

Frequent Contributor
Posts: 102

Re: keep if variable contains....

Thank you so much! this is very helpful.

just one more qucik question if u dont mind:

*if i do them seperately, keep the ones with HK/CN;

data sortedbase4;

set sortedbase2;

if countrycode NE 'CN' then delete;

run;

data sortedbase4;

set sortedbase2;

if countrycode NE 'HK' then delete;

run;

and then try to append them....by

data sortbase3;

  update sortedbase3 sortbase4;

  by countrycode;

run;

i get an error. could u shed some light? thank you!!

Super User
Super User
Posts: 6,500

Re: keep if variable contains....

You have a typo in your dataset names such that you are created SORTEDBASE4 twice and not creating SORTEDBASE3.

You want to use a SET statement instead of an UPDATE.  UPDATE is used to apply transactions to a master dataset.

Contributor
Posts: 43

Re: keep if variable contains....

if find( ISIN,'HK','i')>0 & find(ISIN, 'CN', 'i')>0;


Replace & with OR in your code, you'll get it right!


'i' is optional and it means to ignore the case of the string. Means whether HK or CN is in lower or upper case, that'll work irrespective of the case.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 344 views
  • 0 likes
  • 4 in conversation