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;
you need to use " " when working with character variables:
changing
if countrycode ne CN then delete;
to
if countrycode ne 'CN' then delete ;
you need to use " " when working with character variables:
changing
if countrycode ne CN then delete;
to
if countrycode ne 'CN' then delete ;
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;
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.
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.
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;
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;
data sortedbase2;
set sortedbase;
where substr(left(ISIN),1,2) in ("HK","CN");
run;
hi ... the more the merrier ...
data sortedbase3;
array check(2) $2 _temporary_ ("HK" "CN");
set sortedbase2;
if countrycode in check;
run;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.