- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
you need to use " " when working with character variables:
changing
if countrycode ne CN then delete;
to
if countrycode ne 'CN' then delete ;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
you need to use " " when working with character variables:
changing
if countrycode ne CN then delete;
to
if countrycode ne 'CN' then delete ;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
data sortedbase2;
set sortedbase;
where substr(left(ISIN),1,2) in ("HK","CN");
run;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
hi ... the more the merrier ...
data sortedbase3;
array check(2) $2 _temporary_ ("HK" "CN");
set sortedbase2;
if countrycode in check;
run;