DATA Step, Macro, Functions and more

Coding with the use of if/else statement

Reply
Contributor
Posts: 21

Coding with the use of if/else statement

Can anyone help with this code. I am trying to use an if/else statement to combine several variables into one. The variable is created in the new dataset but the data is not coding based on the specifications made in the if/else statement. Please see my code and a sample observation from the data set below:

 

data service;
set weekone;
length svc $2;
if
Service1="Onco" and
Service2="Primary" and
Service4="NP" and
Service5="Nurse" and
Service6="SWCM" and
Service9="PN" and
Service10="FS" and
Service13="nutrition" then svc="01";
else if
Service1="Onco" and
Service5="Nurse" and
Service6="SWCM" and
Service9="PN" and
Service12="PT" and
Service13="nutrition" and
Service14="ET" then svc="02";
else if
Service1="Onco" and
Service4="NP" and
Service6="SWCM" and
Service10="FS" and
Service12="PT" and
Service13="nutrition" then svc="03";
else if
Service1="Onco" and
Service6="SWCM" and
Service7="psych" and
Service9="PN" and
Service12="PT" and
Service13="nutrition" then svc="04";
else if
Service1="Onco" and
Service4="NP" and
Service5="nurse" and
Service6="SWCM" and
Service9="PN" and
Service13="nutrition" then svc="05";
else if
Service1="Onco" and
Service5="Nurse" and
Service12="PT" and
Service13="nutrition" and
Service15="Other" then svc="06";
else if
Service1="Onco" and
Service4="NP" and
Service6="SWCM" and
Service12="PT" and
Service13="Nutrition" then svc="07";
else if
Service1="Onco" and
Service6="SWCM" and
Service9="PN" and
Service13="Nutrition" and
Service15="Other" then svc="08";
else if
Service1="Onco" and
Service4="NP" and
Service5="nurse" and
Service9="PN" and
Service15="Other" then svc="09";
else if
Service1="Onco" and
Service3="Surg" and
Service4="NP" and
Service5="nurse" and
Service9="PN" then svc="10";
else if
Service2="Primary" and
Service3="surg" and
Service6="SWCM" and
Service7="psych" and
Service11="Survivor" then svc="11";
else if
Service4="NP" and
Service5="Nurse" and
Service6="SWCM" and
Service12="PT" and
Service13="nutrition" then svc="12";
else if
Service4="NP" and
Service5="nurse" and
Service6="SWCM" and
Service9="PN" and
Service13="nutrition" then svc="13";
else if
Service5="nurse" and
Service6="SWCM" and
Service9="PN" and
Service13="nutrition" and
Service15="Other" then svc="14";
else if
Service5="nurse" and
Service6="SWCM" and
Service12="PT" and
Service13="nutrition" and
Service15="Other" then svc="15";
else if
Service6="SWCM" and
Service9="PN" and
Service12="PT" and
Service13="nutrition" and
Service15="Other" then svc="16";
else if
Service1= "Onco" and
Service5= "nurse" and
Service9= "PN" and
Service15="Other" then svc="17";
else if
Service1="Onco" and
Service4="NP" and
Service5="Nurse" and
Service15="Other" then svc="18";
else if
Service1="Onco" and
Service5="Nurse" and
Service8="HB" and
Service13="nutrition" then svc="19";
else if
Service1="Onco" and
Service4="NP" and
Service5="Nurse" and
Service6="SWCM" then svc="20";
else if
Service1="Onco" and
Service4="NP" and
Service6="SWCM" and
Service13="nutrition" then svc="21";
else if
Service1="Onco" and
Service5="Nurse" and
Service9="PN" and
Service13="nutrition" then svc="22";
else if
Service1="Onco" and
Service4="NP" and
Service5="nurse" and
service13="nutrition" then svc="23";
else if
Service1="Onco" and
Service5="nurse" and
Service11="Survivor" and
Service15="Other" then svc="24";
else if
Service1="Onco" and
Service5="nurse" and
Service6="SWCM" and
Service9="PN" then svc="25";
else if
Service1="Onco" and
Service4="NP" and
Service9="PN" and
Service13="nutrition" then svc="26";
else if
Service4="NP" and
Service6="SWCM" and
Service9="PN" and
Service13="nutrition" then svc="27";
else if
Service4="NP" and
Service6="SWCM" and
Service12="PT" and
Service13="nutrition" then svc="28";
else if
Service4="NP" and
Service5="nurse" and
Service6="SWCM" and
Service13="nutrition" then svc="29";
else if
Service5="nurse" and
Service6="SWCM" and
Service9="PN" and
Service13="nutrition" then svc="30";
else if
Service5="nurse" and
Service6="SWCM" and
Service12="PT" and
Service15="Other" then svc="31";
else if
Service6="SWCM" and
Service12="PT" and
Service13="nutrition" and
Service15="Other" then svc="32";
else if
Service9="PN" and
Service12="PT" and
Service13="nutrition" and
Service15="Other" then svc="33";
else if
service1= "Onco" and
service5= "nurse" and
service15= "other" then svc= "34";
else if
Service1= "Onco" and
Service3= "Surg" and
Service9= "PN" then svc= "35";

else if
Service1= "Onco" and
Service9= "PN" and
Service15= "Other" then svc="36";
else if
Service1="Onco" and
Service5="Nurse" and
Service6="SWCM" then svc="37";
else if
Service1="Onco" and
Service2="Primary" and
Service3="surg" then svc="38";
else if
Service1="Onco" and
Service4="NP" and
Service9="PN" then svc="39";
else if
Service1="Onco" and
Service5="Nurse" and
Service9="PN" then svc="40";
else if
Service1="Onco" and
Service9="PN" and
Service13="nutrition" then svc="41";
else if
Service1="Onco" and
Service4="NP" and
Service15="Other" then svc="42";
else if
Service1="Onco" and
Service2="primary" and
Service5="nurse" then svc="43";
else if
Service1="Onco" and
Service4="NP" and
Service5="nurse" then svc="44";
else if
Service1="Onco" and
Service5="nurse" and
Service13="nutrition" then svc="45";
else if
Service1="Onco" and
Service8="HB" and
Service15="Other" then svc="46";
else if
Service4="NP" and
Service9="PN" and
Service15="Other" then svc="47";
else if
Service4="NP" and
Service9="PN" and
Service13="nutrition" then svc="48";
else if
Service4="NP" and
Service5="nurse" and
Service6="SWCM" then svc="49";
else if
Service4="NP" and
Service6="SWCM" and
Service13="nutrition" then svc="50";
else if
Service4="NP" and
Service12="PT" and
Service13="nutrition" then svc="51";
else if
Service4="NP" and
Service5="nurse" and
Service9="PN" then svc="52";
else if
Service4="NP" and
Service5="nurse" and
Service13="nutrition" then svc="53";
else if
Service5="nurse" and
Service6="SWCM" and
Service13="nutrition" then svc="54";
else if
Service5="nurse" and
Service6="SWCM" and
Service9="PN" then svc="55";
else if
Service5="nurse" and
Service9="PN" and
Service15="Other" then svc="56";
else if
Service6="SWCM" and
Service9="PN" and
Service15="Other" then svc="57";
else if
Service6="SWCM" and
Service13="nutrition" and
Service15="Other" then svc="58";
else if
Service12="PT" and
Service13="nutrition" and
Service15="Other" then svc="59";
else if
Service1= "Onco" and
Service3= "Surg" then svc= "60";
else if
Service1="Onco" and
Service15="Other" then svc="61";
else if
Service1="Onco" and
Service9="PN" then svc="62";
else if
Service1="Onco" and
Service6="SWCM" then svc="63";
else if
Service1="Onco" and
Service5="nurse" then svc="64";
else if
Service1="Onco" and
Service4="NP" then svc="65";
else if
Service3="Surg" and
Service5="nurse" then svc="66";
else if
Service3="Surg" and
Service15="Other" then svc="67";
else if
Service4="NP" and
Service9="PN" then svc="68";
else if
Service4="NP" and
Service6="SWCM" then svc="69";
else if
Service4="NP" and
Service13="nutrition" then svc="70";
else if
Service4="NP" and
Service5="Nurse" then svc="71";
else if
Service4="NP" and
Service15="Other" then svc="72";
else if
Service5="nurse" and
Service6="SWCM" then svc="73";
else if
Service5="nurse" and
Service9="PN" then svc="74";
else if
Service5="nurse" and
Service13="nutrition" then svc="75";
else if
Service5="nurse" and
Service15="Other" then svc="76";
else if
Service5="nurse" and
Service15="Other" then svc="77";
else if
Service6="SWCM" and
Service9="PN" then svc="78";
else if
Service7="psych" and
Service9="PN" then svc="79";
else if
Service9="PN" and
Service12="PT" then svc="80";
else if
Service9="PN" and
Service15="Other" then svc="81";
else if
Service9="PN" and
Service10="FS" then svc="82";
else if
Service1="Onco" then Svc="83";
else if
Service2="Primary" then svc="84";
else if
Service4="NP" then svc="85";
else if
Service5="nurse" then svc="86";
else if
Service9="PN" then svc="87";
else if
Service15="Other" then svc="88"; run;

 

SAS Output


Obs record_id compldate_1wksurvey s2_q1___1 s2_q1___2 s2_q1___3 s2_q1___4 s2_q1___5 s2_q1___6 s2_q1___7 s2_q1___8 s2_q1___9 s2_q1___10 s2_q1___11 s2_q1___12 s2_q1___13 s2_q1___14 s2_q1___15 otherprovider s2_q2 s2_q3 s2_q4 s2_q5 s2_q6 s2_q7 s2_q8 s2_q9 s2_q10 s2_q11 s2_q12 s2_q13 s2_q14 s2_q15 s2_q16 s2_q17 s2_q18 s2_q19 s2_q20 s2_q21 s2_q22 s2_q23 s2_q24 s2_q25 s2_q26 s2_q27 s2_q28 s2_q29 s2_q30 s2_q31 s2_q32 s2_q33 s2_q34 s2_q35 s2_q36 s2_q37 s2_q38 s2_q39 s2_q40 s2_q41 s2_q42 s2_q43 s2_q44 s2_q45 s2_q46 s2_q47 s2_q48 s2_q49 s2_q50 s2_q51 s2_q52 s2_q53 s2_q54 s2_q55 s2_q56 s2_q57 s2_q58 s2_q59 s2_q60 week_postvisit_follo_v_0 service1 service2 service3 service4 service5 service6 service7 service8 service9 service10 service11 service12 service13 service14 service15 svc 925
104213MAY2016000110000000000 0:25:00YesNo, definitely notNot applicableYes, definitelyDiscussed it with me in detailNo, definitely notDid not discuss itNot applicableSomewhatYes, somewhatYes, definitelyNo, definitely notYes, somewhatNo, definitely notBriefly discussed it with meNo, definitely notNo, definitely notNo, definitely notNo, definitely notNo, definitely notDid not discuss itDid not discuss itNo, definitely notExtremelyYes, definitelyYes, definitelyNo, definitely notYes, somewhatYes, definitelyYes, definitelyYes, definitelyYes, somewhatExtremelyYes, definitelyExtremelyExtremelyYes, definitelyNo, definitely notNo, definitely notExtremelyNo, definitely notYes, definitelyYes, somewhatYes, definitelyYes, definitelyDon't knowYes, somewhatYes, somewhat7Agree33531555Complete   NPnurse          85
Super User
Posts: 11,343

Re: Coding with the use of if/else statement

Posted in reply to malikah_sph

You would have to provide examples of data involved in the If, the result for your new variable and the actual result.

The data you pasted, besides being in a format that the forum doesn't like very well included dozens of variables that are not involved in this issue.

 

This link shows a way to create a data step that we could use to test data/compare results that is simple text that can be posted into a code box (the one that comes when you click on the "running man" Icon)

https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat...

Use a Keep statement for the data set for just the SERVICE variables and SVC.

 

But I suspect the first order error is related to spelling. Please look at this:

else if
Service4="NP" and
Service5="Nurse" and
Service6="SWCM" and
Service12="PT" and
Service13="nutrition" then svc="12";
else if
Service4="NP" and
Service5="nurse" and
Service6="SWCM" and
Service9="PN" and
Service13="nutrition" then svc="13";
else if

You have Service5 testing for "Nurse" and "nurse". I suspect that is not correct. And others may exist. Or your data has other values(very likely if people entered it and it hasn't been cleaned or standardized).

 

You might want to consider Upcase for the variables and comparison strings.

 

The next would likely be order of assignment.

Super User
Posts: 5,441

Re: Coding with the use of if/else statement

Posted in reply to malikah_sph
I think this is a school book example what happens when you don't store your data in a structured way - it leads to an unmanageable data base.
Normalise your data and many problems similar to this will vanish.
Data never sleeps
Super User
Posts: 19,873

Re: Coding with the use of if/else statement

Posted in reply to malikah_sph

You seem to have many if/else-if. 

Can you break it into more groups?

 

if service1='Onco' then do;

    *remaining if groups here;

else if service4='NP' then do;

   *remaining if conditions here;

 

 

Usually I hate nested if/else but in this case it may make things a bit easier to debug. 

To debug this, find the IF conditions that aren't working and then change them to do/end and add in PUT statements to help you debug. This way you can see where the code is not meeting the condition you expect it and you can play around with the condition until it does.

If you need to debug a condition add multiple if/then/do with each if only testing a single condition at a time. It's tedious but one way of debugging code that doesn't work as expected.

 

if <condition> then do;

put 'Condition X met';

<set value code>;

end;

Ask a Question
Discussion stats
  • 3 replies
  • 234 views
  • 0 likes
  • 4 in conversation