<?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 Assign values to new variable only if source variable missing in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Assign-values-to-new-variable-only-if-source-variable-missing/m-p/853838#M337470</link>
    <description>&lt;P&gt;Hello and Thank you!&lt;/P&gt;&lt;P&gt;I wanted to see if could create a new variable with values of a another variable and values from a range. I only want to replace missing values, and without duplicates.&lt;BR /&gt;For instance my code looks like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;P&gt;data have;&lt;BR /&gt;infile datalines dlm=',';&lt;BR /&gt;input blah $ var1 var2 ;&lt;BR /&gt;datalines;&lt;BR /&gt;A, 1, 2,&amp;nbsp;&lt;BR /&gt;b, 3, 2,&amp;nbsp;&lt;BR /&gt;c, 3, .,&lt;BR /&gt;d, 8, .,&amp;nbsp;&lt;BR /&gt;e, 5, .,&amp;nbsp;&lt;BR /&gt;f, 7, 2,&lt;BR /&gt;g, 3, 1,&amp;nbsp;&lt;BR /&gt;h, 9, 4,&amp;nbsp;&lt;BR /&gt;i, 8, .&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data want;&lt;BR /&gt;set have;&lt;/P&gt;&lt;P&gt;If var2 ne . then var3=var2;&lt;BR /&gt;else if missing(var2) then var3=rand("integer", 1800, 1803);&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;What I want is this :&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="JMagenta_1-1673653214319.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/79393i3B56F993322B3A1C/image-size/medium?v=v2&amp;amp;px=400" role="button" title="JMagenta_1-1673653214319.png" alt="JMagenta_1-1673653214319.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;gt; What I get is this:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="JMagenta_0-1673653029168.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/79392i9E35F20F38005757/image-size/medium?v=v2&amp;amp;px=400" role="button" title="JMagenta_0-1673653029168.png" alt="JMagenta_0-1673653029168.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How can I end this pain? I know I need a do loop of some sort and/or an array maybe. How should I set it up? I have a much longer dataset and I don't want to assign duplicate values to this new variable. I know the answe&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Thank you!!!&lt;/P&gt;</description>
    <pubDate>Fri, 13 Jan 2023 23:46:14 GMT</pubDate>
    <dc:creator>JMagenta</dc:creator>
    <dc:date>2023-01-13T23:46:14Z</dc:date>
    <item>
      <title>Assign values to new variable only if source variable missing</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assign-values-to-new-variable-only-if-source-variable-missing/m-p/853838#M337470</link>
      <description>&lt;P&gt;Hello and Thank you!&lt;/P&gt;&lt;P&gt;I wanted to see if could create a new variable with values of a another variable and values from a range. I only want to replace missing values, and without duplicates.&lt;BR /&gt;For instance my code looks like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;P&gt;data have;&lt;BR /&gt;infile datalines dlm=',';&lt;BR /&gt;input blah $ var1 var2 ;&lt;BR /&gt;datalines;&lt;BR /&gt;A, 1, 2,&amp;nbsp;&lt;BR /&gt;b, 3, 2,&amp;nbsp;&lt;BR /&gt;c, 3, .,&lt;BR /&gt;d, 8, .,&amp;nbsp;&lt;BR /&gt;e, 5, .,&amp;nbsp;&lt;BR /&gt;f, 7, 2,&lt;BR /&gt;g, 3, 1,&amp;nbsp;&lt;BR /&gt;h, 9, 4,&amp;nbsp;&lt;BR /&gt;i, 8, .&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data want;&lt;BR /&gt;set have;&lt;/P&gt;&lt;P&gt;If var2 ne . then var3=var2;&lt;BR /&gt;else if missing(var2) then var3=rand("integer", 1800, 1803);&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;What I want is this :&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="JMagenta_1-1673653214319.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/79393i3B56F993322B3A1C/image-size/medium?v=v2&amp;amp;px=400" role="button" title="JMagenta_1-1673653214319.png" alt="JMagenta_1-1673653214319.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;gt; What I get is this:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="JMagenta_0-1673653029168.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/79392i9E35F20F38005757/image-size/medium?v=v2&amp;amp;px=400" role="button" title="JMagenta_0-1673653029168.png" alt="JMagenta_0-1673653029168.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How can I end this pain? I know I need a do loop of some sort and/or an array maybe. How should I set it up? I have a much longer dataset and I don't want to assign duplicate values to this new variable. I know the answe&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Thank you!!!&lt;/P&gt;</description>
      <pubDate>Fri, 13 Jan 2023 23:46:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assign-values-to-new-variable-only-if-source-variable-missing/m-p/853838#M337470</guid>
      <dc:creator>JMagenta</dc:creator>
      <dc:date>2023-01-13T23:46:14Z</dc:date>
    </item>
    <item>
      <title>Re: Assign values to new variable only if source variable missing</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assign-values-to-new-variable-only-if-source-variable-missing/m-p/853853#M337476</link>
      <description>&lt;P&gt;Welcome to the SAS communities.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Below two options how you could do this.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want(drop=_cnt);
  set have;

  if var2 ne . then var3=var2;
  else var3=1800+_n_;

  if var2 ne .  then var4=var2;
  else 
    do;
      _cnt+1;
      var4=1800+_cnt;
    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="Patrick_0-1673664008121.png" style="width: 307px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/79398iEFCA1C8C80C96CDE/image-dimensions/307x265?v=v2" width="307" height="265" role="button" title="Patrick_0-1673664008121.png" alt="Patrick_0-1673664008121.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Using a random function like in your code will create random values which then obviously won't be sequential AND which also could repeat.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Option 1 in the code I've posted uses automatic variable _n_ which contains the observation number and though is sequential and unique per row.&lt;/P&gt;
&lt;P&gt;Option 2 creates counter variable _cnt. Using SAS syntax _cnt+1; on the left side of the equal sign is "specialty" syntax to create and populate a variable that will retain values. This syntax is often used for any sort of counter variables than need to increase over multiple observations.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 14 Jan 2023 02:44:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assign-values-to-new-variable-only-if-source-variable-missing/m-p/853853#M337476</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2023-01-14T02:44:05Z</dc:date>
    </item>
    <item>
      <title>Re: Assign values to new variable only if source variable missing</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assign-values-to-new-variable-only-if-source-variable-missing/m-p/853868#M337483</link>
      <description>&lt;P&gt;If you just want the values to be unique, here is a simple solution:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  var3=coalesce(var2,1800+_N_);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;However, if you want to increase the value filled in only when it is actually used, it goes like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  retain _new 1800;
  if missing(var2) then do;
    _new+1;
    var3=_new;
    end;
  else
    var3=var2;
  drop _new;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 14 Jan 2023 10:50:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assign-values-to-new-variable-only-if-source-variable-missing/m-p/853868#M337483</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2023-01-14T10:50:50Z</dc:date>
    </item>
    <item>
      <title>Re: Assign values to new variable only if source variable missing</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assign-values-to-new-variable-only-if-source-variable-missing/m-p/854129#M337566</link>
      <description>&lt;P&gt;Worked like a charm, thank you!&lt;/P&gt;</description>
      <pubDate>Tue, 17 Jan 2023 16:22:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assign-values-to-new-variable-only-if-source-variable-missing/m-p/854129#M337566</guid>
      <dc:creator>JMagenta</dc:creator>
      <dc:date>2023-01-17T16:22:54Z</dc:date>
    </item>
  </channel>
</rss>

