<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: re: assign department codes in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/re-assign-department-codes/m-p/748014#M234878</link>
    <description>&lt;P&gt;Here's a &lt;EM&gt;start&lt;/EM&gt;, below.&amp;nbsp; This probably is &lt;STRONG&gt;NOT&lt;/STRONG&gt; what you want, at least not completely.&amp;nbsp; I'm just trying to give you some ideas.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1.&amp;nbsp; I have joined the IF statements together with an ELSE so that they are mutually exclusive.&lt;/P&gt;
&lt;P&gt;2.&amp;nbsp; I have a final ELSE so that if no condition is met, we'll see an "unkn" (unknown) in the results so at least we'll know what's going on.&lt;/P&gt;
&lt;P&gt;3.&amp;nbsp; I coded an action for 'ES'.&amp;nbsp; This may be what you want, so correct as necessary.&lt;/P&gt;
&lt;P&gt;4.&amp;nbsp; As an example, I broke apart the Department and the Dpt IF statements for LM/HRLD.&amp;nbsp; The way they were, if the Dpt was missing, you got nothing.&amp;nbsp; I set up a default in the case that the Dpt is not 16.&amp;nbsp; This may not be what you want; this is just an example.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In an IF statement, you need to handle every possible combination of conditions -- and handle what happens if none of the conditions are met.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The results are below the code.&amp;nbsp; Note that now all the rows have Department1 populated.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data Want;
    length Department1 $ 4 Dpt1 8;
    format Department1 $char4.;
	set Have;
	by StudentUID Term;
			if Department in ('LM','HRLD') then do;
				IF  Dpt	= 16  then
					do;
						Department1 = 'HRLD';
						Dpt1=16;		
					end;
				else
					do;
						Department1 = Department;
						Dpt1 = Dpt;
					end;
			end;
			ELSE
			if Department in ('IPG') then do;
					Department1 = 'IPG';
					Dpt1=22;
			end;
			ELSE
			if Department in ('PT','PT2','PTD') then do;
					Department1 = 'PT';
					Dpt1=53;
			end;
			ELSE
			IF	Department	=	'ES'	THEN
				DO;
					Department1 = 'ES';
					Dpt1 = 54;
				END;
			ELSE
				DO;
					Department1 = 'Unkn';
					Dpt1 = 99;
				END;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="jimbarbour_0-1623722365729.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/60387i5C08A6F4E439AFC8/image-size/large?v=v2&amp;amp;px=999" role="button" title="jimbarbour_0-1623722365729.png" alt="jimbarbour_0-1623722365729.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Jim&lt;/P&gt;</description>
    <pubDate>Tue, 15 Jun 2021 01:59:37 GMT</pubDate>
    <dc:creator>jimbarbour</dc:creator>
    <dc:date>2021-06-15T01:59:37Z</dc:date>
    <item>
      <title>re: assign department codes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/re-assign-department-codes/m-p/748003#M234872</link>
      <description>&lt;P&gt;Hi...I am trying to create a new variable Department1 based on the variable Dpt and the Department codes in the list. For example, Department codes 'LM' and 'HRLD' would have Department1 = 'HRLD' which is grouped by StudentUID and Term. I can't seem to get it to populate the dataset for all records. Any suggestions?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data Have;
    length StudentUID 8 Term $ 5 Dpt 8 Department $ 4;
	infile datalines4 dlm=',' missover dsd;
    input StudentUID :best32. Term :$char5. Dpt :best32. Department :$char4.;
datalines4;
127389,19-20,16,HRLD
127389,19-20,22,IPG
127389,19-20, ,LM
127389,20-21,54,ES
127389,20-21,16,HRLD
127389,20-21, ,LM
127389,20-21,53,PT
;;;;

data Want;
    length Department1 $ 4 Dpt1 8;
    format Department1 $char4.;
	set Have;
	by StudentUID Term;
			if Dpt=16 and Department in ('LM','HRLD') then do;
					Department1 = 'HRLD';
					Dpt1=16;
			end;
			if Dpt=22 and Department in ('IPG') then do;
					Department1 = 'IPG';
					Dpt1=22;
			end;
			if Dpt=53 and Department in ('PT','PT2','PTD') then do;
					Department1 = 'PT';
					Dpt1=53;
			end;
run;

Want:
StudentUID	Term	DPT	Department	DPT1	Department1
127389	19-20	16	HRLD	16	HRLD
127389	19-20	22	IPG	22	IPG
127389	19-20		LM	16	HRLD
127389	20-21	54	ES	54	ES
127389	20-21	16	HRLD	16	HRLD
127389	20-21		LM	16	HRLD
127389	20-21	53	PT	53	PT
&lt;/PRE&gt;</description>
      <pubDate>Tue, 15 Jun 2021 01:58:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/re-assign-department-codes/m-p/748003#M234872</guid>
      <dc:creator>twildone</dc:creator>
      <dc:date>2021-06-15T01:58:07Z</dc:date>
    </item>
    <item>
      <title>Re: re: assign department codes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/re-assign-department-codes/m-p/748008#M234875</link>
      <description>&lt;P&gt;You're getting missing values because&lt;/P&gt;
&lt;P&gt;1.&amp;nbsp; You have missing values in a numeric field (Dpt)&lt;/P&gt;
&lt;P&gt;and&lt;/P&gt;
&lt;P&gt;2.&amp;nbsp; Because you have no logic for Department = ES&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You need to tell SAS what to do if there is a missing value in DPT, and you need to tell SAS how to handle Department=ES.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Jim&lt;/P&gt;</description>
      <pubDate>Tue, 15 Jun 2021 01:40:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/re-assign-department-codes/m-p/748008#M234875</guid>
      <dc:creator>jimbarbour</dc:creator>
      <dc:date>2021-06-15T01:40:56Z</dc:date>
    </item>
    <item>
      <title>Re: re: assign department codes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/re-assign-department-codes/m-p/748014#M234878</link>
      <description>&lt;P&gt;Here's a &lt;EM&gt;start&lt;/EM&gt;, below.&amp;nbsp; This probably is &lt;STRONG&gt;NOT&lt;/STRONG&gt; what you want, at least not completely.&amp;nbsp; I'm just trying to give you some ideas.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1.&amp;nbsp; I have joined the IF statements together with an ELSE so that they are mutually exclusive.&lt;/P&gt;
&lt;P&gt;2.&amp;nbsp; I have a final ELSE so that if no condition is met, we'll see an "unkn" (unknown) in the results so at least we'll know what's going on.&lt;/P&gt;
&lt;P&gt;3.&amp;nbsp; I coded an action for 'ES'.&amp;nbsp; This may be what you want, so correct as necessary.&lt;/P&gt;
&lt;P&gt;4.&amp;nbsp; As an example, I broke apart the Department and the Dpt IF statements for LM/HRLD.&amp;nbsp; The way they were, if the Dpt was missing, you got nothing.&amp;nbsp; I set up a default in the case that the Dpt is not 16.&amp;nbsp; This may not be what you want; this is just an example.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In an IF statement, you need to handle every possible combination of conditions -- and handle what happens if none of the conditions are met.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The results are below the code.&amp;nbsp; Note that now all the rows have Department1 populated.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data Want;
    length Department1 $ 4 Dpt1 8;
    format Department1 $char4.;
	set Have;
	by StudentUID Term;
			if Department in ('LM','HRLD') then do;
				IF  Dpt	= 16  then
					do;
						Department1 = 'HRLD';
						Dpt1=16;		
					end;
				else
					do;
						Department1 = Department;
						Dpt1 = Dpt;
					end;
			end;
			ELSE
			if Department in ('IPG') then do;
					Department1 = 'IPG';
					Dpt1=22;
			end;
			ELSE
			if Department in ('PT','PT2','PTD') then do;
					Department1 = 'PT';
					Dpt1=53;
			end;
			ELSE
			IF	Department	=	'ES'	THEN
				DO;
					Department1 = 'ES';
					Dpt1 = 54;
				END;
			ELSE
				DO;
					Department1 = 'Unkn';
					Dpt1 = 99;
				END;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="jimbarbour_0-1623722365729.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/60387i5C08A6F4E439AFC8/image-size/large?v=v2&amp;amp;px=999" role="button" title="jimbarbour_0-1623722365729.png" alt="jimbarbour_0-1623722365729.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Jim&lt;/P&gt;</description>
      <pubDate>Tue, 15 Jun 2021 01:59:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/re-assign-department-codes/m-p/748014#M234878</guid>
      <dc:creator>jimbarbour</dc:creator>
      <dc:date>2021-06-15T01:59:37Z</dc:date>
    </item>
  </channel>
</rss>

