Help using Base SAS procedures

want to keep observations if variable x equals to something

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 102
Accepted Solution

want to keep observations if variable x equals to something

hi i have a variable country code (below)

data sortedbase2;

set sortedbase;

  length countrycode $2;

  countrycode = substr(left(ISIN),1,2);  /* left-align ISIN and extract 2 characters starting from position 1 of ‘ISIN’ */

run;

and I want to keep them is country code equals both HK and CN. how may i code this?





-----------------------

i get an error for somereason and sas creates a variable HK and CN if ''

*keep the ones with HK/CN;

data sortedbase3;

set sortedbase2;

if countrycode ne CN then delete;

run;


Accepted Solutions
Solution
‎04-16-2014 04:24 PM
Super Contributor
Posts: 1,636

Re: want to keep observations if variable x equals to something

you need to use " " when working with character variables:

changing

if countrycode ne CN then delete;

to

if countrycode ne 'CN' then delete ;

View solution in original post


All Replies
Solution
‎04-16-2014 04:24 PM
Super Contributor
Posts: 1,636

Re: want to keep observations if variable x equals to something

you need to use " " when working with character variables:

changing

if countrycode ne CN then delete;

to

if countrycode ne 'CN' then delete ;

Trusted Advisor
Posts: 1,932

Re: want to keep observations if variable x equals to something

and I want to keep them is country code equals both HK and CN

Do you perhaps mean "and I want to keep them if country code equals either HK or CN"? Because based on what you wrote, I don't think country code can equal both HK and CN

If so, then this should work

data sortedbase2;

  set sortedbase;

  length countrycode $2;

  countrycode = substr(left(ISIN),1,2);  /* left-align ISIN and extract 2 characters starting from position 1 of ‘ISIN’ */

if countrycode="HK" or countrycode="CN" then output;

run;

Super User
Posts: 11,343

Re: want to keep observations if variable x equals to something

Posted in reply to PaigeMiller

And just in case you end up wanting to use longer lists of values the IN operator is helpful:

if countrycode in ("HK", "CN") then output;

Add items as needed instead of a multitude of If this or that or somethelse or yet some other value constructs.

Super User
Posts: 5,516

Re: want to keep observations if variable x equals to something

In addition to all the other suggestions, you don't need to actually create COUNTRYCODE.  You could use:

data sortedbase2;

   set sortedbase;

   if left(isin) in: ('HK', 'CN') then output;

run;

The in: comparison will use fewer characters of ISIN instead of the whole string.

Contributor SKK
Contributor
Posts: 35

Re: want to keep observations if variable x equals to something

Here's my try with Proc SQL,

PROC SQL;

CREATE TABLE WANT AS SELECT * FROM sortedbase

WHERE substr(left(UPCASE(ISIN)),1,2) in ("HK","CN");

QUIT;

Frequent Contributor
Posts: 78

Re: want to keep observations if variable x equals to something

data sortedbase2;

set sortedbase;

  length countrycode $2;

if substr(compress(upcase(ISIN)),1,2) in ('HK','CN');

  countrycode = substr(compress(upcase(ISIN)),1,2);

run;

Super User
Super User
Posts: 7,993

Re: want to keep observations if variable x equals to something

data sortedbase2;

  set sortedbase;

  where substr(left(ISIN),1,2) in ("HK","CN");

run;

Valued Guide
Posts: 765

Re: want to keep observations if variable x equals to something

hi ... the more the merrier ...

data sortedbase3;

array check(2) $2 _temporary_ ("HK" "CN");

set sortedbase2;

if countrycode in check;

run;

🔒 This topic is solved and locked.

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

Discussion stats
  • 8 replies
  • 4588 views
  • 14 likes
  • 9 in conversation