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

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;

 

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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;

View solution in original post

3 REPLIES 3
gamotte
Rhodochrosite | Level 12

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 ?

Kurt_Bremser
Super User

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.

Astounding
PROC Star

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;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

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.

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
  • 3 replies
  • 415 views
  • 0 likes
  • 4 in conversation