<?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 Help with SAS retain code in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Help-with-SAS-retain-code/m-p/927922#M365116</link>
    <description>&lt;P&gt;&lt;SPAN&gt;I'm having difficulty writing a piece of SAS code&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;and&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;I'm&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp;seeking the most effective approach within SAS. I have a variable named 'type' (with values 1, 2, 3, 4) and client IDs. Essentially, I aim to create another variable called 'modified_type,' which follows these rules:&lt;/SPAN&gt;&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;&lt;SPAN&gt;If there's a&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;single&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp;unique client ID with only 1 in the 'type' variable, then 'modified_type' should be set to 0.&lt;/SPAN&gt;&lt;/LI&gt;&lt;LI&gt;&lt;SPAN&gt;However, if&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;there are&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp;multiple client IDs with the same number&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;having&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp;1 in the 'type' variable, 'modified_type' should be set to 1.&lt;/SPAN&gt;&lt;/LI&gt;&lt;LI&gt;&lt;SPAN&gt;If there are 2 in the 'type' variable, then 'modified_type' should be set to 2... and so on.&lt;BR /&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;&lt;SPAN&gt;I tried using the retain function so something like this:&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;retain modified_type 0;&lt;BR /&gt;if first.client_id then modified_type = 0;&lt;/P&gt;&lt;P&gt;if type = '1' or type = '1st' then modified_type = 1;&lt;BR /&gt;else if type = '2' or = '2nd' then modified_type = 2;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;and so on.....&lt;BR /&gt;but these does seem to retain 0 in the type and for some reason shows as 1.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Yea im note sure the best way to do this, a newbie at SAS so would even appreciate the point in the right direction:&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;</description>
    <pubDate>Fri, 10 May 2024 19:05:41 GMT</pubDate>
    <dc:creator>maple_tandoori</dc:creator>
    <dc:date>2024-05-10T19:05:41Z</dc:date>
    <item>
      <title>Help with SAS retain code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-SAS-retain-code/m-p/927922#M365116</link>
      <description>&lt;P&gt;&lt;SPAN&gt;I'm having difficulty writing a piece of SAS code&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;and&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;I'm&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp;seeking the most effective approach within SAS. I have a variable named 'type' (with values 1, 2, 3, 4) and client IDs. Essentially, I aim to create another variable called 'modified_type,' which follows these rules:&lt;/SPAN&gt;&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;&lt;SPAN&gt;If there's a&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;single&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp;unique client ID with only 1 in the 'type' variable, then 'modified_type' should be set to 0.&lt;/SPAN&gt;&lt;/LI&gt;&lt;LI&gt;&lt;SPAN&gt;However, if&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;there are&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp;multiple client IDs with the same number&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;having&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp;1 in the 'type' variable, 'modified_type' should be set to 1.&lt;/SPAN&gt;&lt;/LI&gt;&lt;LI&gt;&lt;SPAN&gt;If there are 2 in the 'type' variable, then 'modified_type' should be set to 2... and so on.&lt;BR /&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;&lt;SPAN&gt;I tried using the retain function so something like this:&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;retain modified_type 0;&lt;BR /&gt;if first.client_id then modified_type = 0;&lt;/P&gt;&lt;P&gt;if type = '1' or type = '1st' then modified_type = 1;&lt;BR /&gt;else if type = '2' or = '2nd' then modified_type = 2;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;and so on.....&lt;BR /&gt;but these does seem to retain 0 in the type and for some reason shows as 1.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Yea im note sure the best way to do this, a newbie at SAS so would even appreciate the point in the right direction:&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 10 May 2024 19:05:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-SAS-retain-code/m-p/927922#M365116</guid>
      <dc:creator>maple_tandoori</dc:creator>
      <dc:date>2024-05-10T19:05:41Z</dc:date>
    </item>
    <item>
      <title>Re: Help with SAS retain code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-SAS-retain-code/m-p/927937#M365118</link>
      <description>&lt;P&gt;Code looks fine.&amp;nbsp; But we will need to see example input data to know if your logic makes sense.&amp;nbsp; Share a simple input dataset and the expected result for that dataset.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note the RETAIN statement just means that SAS will not reset the value to missing when it starts a new iteration of the data step.&amp;nbsp; The 0 in the retain statement means the initial value before the first iteration starts will be 0 instead of missing.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any variable that is coming in from an existing dataset via a SET/MERGE/UPDATE statement is already being retained, but you normally do not notice because the retained value is replaced when the SET/MERGE/UPDATE statement executes.&amp;nbsp; So&amp;nbsp;modified_type should NOT already exist in the source dataset.&lt;/P&gt;</description>
      <pubDate>Fri, 10 May 2024 19:28:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-SAS-retain-code/m-p/927937#M365118</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-05-10T19:28:15Z</dc:date>
    </item>
    <item>
      <title>Re: Help with SAS retain code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-SAS-retain-code/m-p/927959#M365121</link>
      <description>&lt;P&gt;Hi Tom,&lt;BR /&gt;&lt;BR /&gt;Thank you so much for your reply. Thank you for explanation on retain, i think it makes sense for my purposes as i want every clientid that appears only once to have new variable (modified_type) = 0. Please allow me show a example of the sample data and my excepted out to show you what i mean, the modified type is the new variable i want to create:&lt;BR /&gt;&lt;BR /&gt;Client_id&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; type&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; modified type&lt;BR /&gt;&lt;BR /&gt;1122&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; (one here because there is another entry for id 1122)&lt;BR /&gt;1122&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;BR /&gt;1523&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; (0 here because only 1 entry for id 1523)&lt;BR /&gt;2789&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;BR /&gt;2789&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&lt;BR /&gt;2789&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3&lt;BR /&gt;8776&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&lt;BR /&gt;9900&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 11 May 2024 01:47:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-SAS-retain-code/m-p/927959#M365121</guid>
      <dc:creator>maple_tandoori</dc:creator>
      <dc:date>2024-05-11T01:47:00Z</dc:date>
    </item>
    <item>
      <title>Re: Help with SAS retain code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-SAS-retain-code/m-p/927968#M365124</link>
      <description>&lt;P&gt;So you need to also use BY group processing.&lt;/P&gt;
&lt;P&gt;Let's convert your example listing into an actual dataset.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input Client_id type;
cards;
1122 1
1122 1
1523 1
2789 1
2789 2
2789 3
8776 1
9900 1
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now we can set the new variable to 0 when there is only one observation for the BY group (when it is both the first and the last there is only one) and 1 when there are more than one.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  by client_id;
  if first.client_id then new_var=not last.client_id;
  else new_var+1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result&lt;/P&gt;
&lt;PRE&gt;       Client_
Obs       id      type    new_var

 1       1122       1        1
 2       1122       1        2
 3       1523       1        0
 4       2789       1        1
 5       2789       2        2
 6       2789       3        3
 7       8776       1        0
 8       9900       1        0&lt;/PRE&gt;
&lt;P&gt;Note that your example results does not seem to the right result for the second observation for the first client.&lt;/P&gt;</description>
      <pubDate>Sat, 11 May 2024 03:05:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-SAS-retain-code/m-p/927968#M365124</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-05-11T03:05:22Z</dc:date>
    </item>
    <item>
      <title>Re: Help with SAS retain code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-SAS-retain-code/m-p/927969#M365125</link>
      <description>&lt;P&gt;Based on what I understood and your sample data below should do.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  infile datalines dsd dlm=',' truncover;
  input (Client_id type modified_type_desired) (:best32.);
  datalines;
1122,1,1,(one here because there is another entry for id 1122)
1122,1,1
1523,1,0,(0 here because only 1 entry for id 1523)
2789,1,1
2789,2,2
2789,3,3
8776,1,0
9900,1,0
;

data want;
  set have;
  by Client_id;
  if first.Client_id and last.Client_id then modified_type_derived=0;
  else modified_type_derived=type;
run;

proc print data=want;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1715397015855.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/96444iAE1F2B63691167BD/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Patrick_0-1715397015855.png" alt="Patrick_0-1715397015855.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 11 May 2024 03:10:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-SAS-retain-code/m-p/927969#M365125</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2024-05-11T03:10:23Z</dc:date>
    </item>
    <item>
      <title>Re: Help with SAS retain code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-SAS-retain-code/m-p/927989#M365132</link>
      <description>&lt;P&gt;Thank you Patrick, your way works better than my initial retain method.&lt;/P&gt;</description>
      <pubDate>Sat, 11 May 2024 18:43:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-SAS-retain-code/m-p/927989#M365132</guid>
      <dc:creator>maple_tandoori</dc:creator>
      <dc:date>2024-05-11T18:43:22Z</dc:date>
    </item>
  </channel>
</rss>

