Hi all, facing problem about the code below:. When I run it , the result only able to detect ratecde=1000 and ratecde='' (the blank data).
However sas unable to read the variable ratecde='coin'.
For the variable column ratecde. There are many datas whih is including these three ---> 1000,coin and (blank).and other numbers
I set the ratecde to be character variable.
Suppose with the code below, these three ratecde='1000' OR ratecde='coin' OR ratecde='' will not have any results in the cov_type column.
ratecde='1000' and ratecde='' " does the result I want which is without any data in cov_type
However ratecde='coin' have data in cov_type (this is wrong).
Does I type anything wrong in my code?
if ratecde='1000' OR ratecde='coin' OR ratecde='' then do;
if rsktyp in ('FHO','FHW','FHZ') then
cov_type='HH';
else if rsktyp in ('FHH','FHI','FHY') then
cov_type='HO';
else if rsktyp in ('FCL','FCZ','FLP') then
cov_type='Conloss';
else if rsktyp in ('FAZ','FDA','FIA') then
cov_type='IAR';
else if rsktyp in ('FDP','FFD','FFZ','FHX','FMD','FMP') then
cov_type='MD1';
end;
When you examine character values, capitalization is important. These are all different values:
coin
coin
Coin
COIN
So when you look for values that match, you have to use the exact value that appears in your data. This might be overkill, but would be safer:
if ratecde='1000' OR upcase(left(ratecde))='COIN' OR ratecde='' then do;
Hello,
There is nothing wrong with the code you show.
data have;
input ratecde $ rsktyp $;
length cov_type $10.;
if ratecde='1000' OR ratecde='coin' OR ratecde='' then do;
if rsktyp in ('FHO','FHW','FHZ') then cov_type='HH';
else if rsktyp in ('FHH','FHI','FHY') then cov_type='HO';
else if rsktyp in ('FCL','FCZ','FLP') then cov_type='Conloss';
else if rsktyp in ('FAZ','FDA','FIA') then cov_type='IAR';
else if rsktyp in ('FDP','FFD','FFZ','FHX','FMD','FMP') then cov_type='MD1';
end;
cards;
1000 FHO
1000 FHH
1000 FCL
1000 FAZ
1000 FDP
. FHO
. FHH
. FCL
. FAZ
. FDP
coin FHO
coin FHH
coin FCL
coin FAZ
coin FDP
;
run;
Can you provide a complete runnable code (with example data in the form of a data step as above) that illustrates your problem ?
Start by not writing ugly, unmaintainable code:
if ratecde in ('1000','coin','')
then do;
select (rsktyp);
when ('FHO','FHW','FHZ') cov_type = 'HH';
when ('FHH','FHI','FHY') cov_type = 'HO';
when ('FCL','FCZ','FLP') cov_type = 'Conloss';
when ('FAZ','FDA','FIA') cov_type = 'IAR';
when ('FDP','FFD','FFZ','FHX','FMD','FMP') cov_type = 'MD1';
end;
end;
Now the visual structure of the code reflects the logic behind it, and you can make a decision where your code might not reflect your intended logic much more easily.
One thing that looks suspicious to me is not setting a length for cov_type. If
cov_type = 'HH';
is the first occurrence of cov_type in your code, it will be defined as $2, and longer values will be truncated. See Maxim 47.
And, of course, supply usable example data and the expected outcome from it.
When you examine character values, capitalization is important. These are all different values:
coin
coin
Coin
COIN
So when you look for values that match, you have to use the exact value that appears in your data. This might be overkill, but would be safer:
if ratecde='1000' OR upcase(left(ratecde))='COIN' OR ratecde='' then do;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Select SAS Training centers are offering in-person courses. View upcoming courses for: