BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
aarony
Obsidian | Level 7

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;

1 ACCEPTED SOLUTION

Accepted Solutions
Linlin
Lapis Lazuli | Level 10

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

8 REPLIES 8
Linlin
Lapis Lazuli | Level 10

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

changing

if countrycode ne CN then delete;

to

if countrycode ne 'CN' then delete ;

PaigeMiller
Diamond | Level 26
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;

--
Paige Miller
ballardw
Super User

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.

Astounding
PROC Star

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.

SKK
Calcite | Level 5 SKK
Calcite | Level 5

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;

TarunKumar
Pyrite | Level 9

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;

RW9
Diamond | Level 26 RW9
Diamond | Level 26

data sortedbase2;

  set sortedbase;

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

run;

MikeZdeb
Rhodochrosite | Level 12

hi ... the more the merrier ...

data sortedbase3;

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

set sortedbase2;

if countrycode in check;

run;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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