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
1042 | 13MAY2016 | 0 | 0 | 0 | 1 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0:25:00 | Yes | No, definitely not | Not applicable | Yes, definitely | Discussed it with me in detail | No, definitely not | Did not discuss it | Not applicable | Somewhat | Yes, somewhat | Yes, definitely | No, definitely not | Yes, somewhat | No, definitely not | Briefly discussed it with me | No, definitely not | No, definitely not | No, definitely not | No, definitely not | No, definitely not | Did not discuss it | Did not discuss it | No, definitely not | Extremely | Yes, definitely | Yes, definitely | No, definitely not | Yes, somewhat | Yes, definitely | Yes, definitely | Yes, definitely | Yes, somewhat | Extremely | Yes, definitely | Extremely | Extremely | Yes, definitely | No, definitely not | No, definitely not | Extremely | No, definitely not | Yes, definitely | Yes, somewhat | Yes, definitely | Yes, definitely | Don't know | Yes, somewhat | Yes, somewhat | 7 | Agree | 3 | 3 | 5 | 3 | 1 | 5 | 5 | 5 | Complete | NP | nurse | 85 |
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)
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.
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;
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 25. 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.