<?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: can we randomly assign values to data based on id in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/can-we-randomly-assign-values-to-data-based-on-id/m-p/505472#M135362</link>
    <description>&lt;P&gt;Something like:&lt;/P&gt;
&lt;PRE&gt;data want (drop=seq);
  set test;
  retain seq;
  seq=ifn(first.id,1,seq+1); 
  if seq=1 then val=12;
  else if seq=2 then val=15;
  else val=20;
run;&lt;/PRE&gt;</description>
    <pubDate>Thu, 18 Oct 2018 09:12:40 GMT</pubDate>
    <dc:creator>RW9</dc:creator>
    <dc:date>2018-10-18T09:12:40Z</dc:date>
    <item>
      <title>can we randomly assign values to data based on id</title>
      <link>https://communities.sas.com/t5/SAS-Programming/can-we-randomly-assign-values-to-data-based-on-id/m-p/505468#M135359</link>
      <description>&lt;P&gt;i am trying to assign val variable for the data as first id should have val=12 and second id as val=15 and the rest of id with val=20.&lt;/P&gt;&lt;PRE&gt;data test;
input pid $ date $;
cards;
1      1/1/2011   
1      1/1/2011   &lt;BR /&gt;1      1/3/2011 
1      3/4/2011   
2      5/1/2010  
2      6/3/2010 
;
run;&lt;BR /&gt;&lt;BR /&gt;Tried something like below but it did not work.&lt;BR /&gt;&lt;BR /&gt;proc sort data=test;&lt;BR /&gt;by pid date;&lt;BR /&gt;run;&lt;BR /&gt; &lt;BR /&gt;data want;&lt;BR /&gt; set test ;&lt;BR /&gt; by pid ;&lt;BR /&gt; if first.date then val=12;&lt;BR /&gt; seq_id+3;&lt;BR /&gt; if last.date then val=20;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;Need something like below&lt;BR /&gt;data need;&lt;BR /&gt;input pid $ date $ val ;&lt;BR /&gt;cards;&lt;BR /&gt;1 1/1/2011 12&lt;BR /&gt;1 1/1/2011 15&lt;BR /&gt;1 3/4/2011 20&lt;BR /&gt;2 5/1/2010 12&lt;BR /&gt;2 6/3/2010 15&lt;BR /&gt;2 6/4/2010 20&lt;BR /&gt;2 12/4/2010 20&lt;BR /&gt;2 14/4/2010 20&lt;BR /&gt;&lt;BR /&gt;;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;Any guidance please&lt;/PRE&gt;</description>
      <pubDate>Thu, 18 Oct 2018 08:55:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/can-we-randomly-assign-values-to-data-based-on-id/m-p/505468#M135359</guid>
      <dc:creator>noda6003</dc:creator>
      <dc:date>2018-10-18T08:55:40Z</dc:date>
    </item>
    <item>
      <title>Re: can we randomly assign values to data based on id</title>
      <link>https://communities.sas.com/t5/SAS-Programming/can-we-randomly-assign-values-to-data-based-on-id/m-p/505472#M135362</link>
      <description>&lt;P&gt;Something like:&lt;/P&gt;
&lt;PRE&gt;data want (drop=seq);
  set test;
  retain seq;
  seq=ifn(first.id,1,seq+1); 
  if seq=1 then val=12;
  else if seq=2 then val=15;
  else val=20;
run;&lt;/PRE&gt;</description>
      <pubDate>Thu, 18 Oct 2018 09:12:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/can-we-randomly-assign-values-to-data-based-on-id/m-p/505472#M135362</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-10-18T09:12:40Z</dc:date>
    </item>
    <item>
      <title>Re: can we randomly assign values to data based on id</title>
      <link>https://communities.sas.com/t5/SAS-Programming/can-we-randomly-assign-values-to-data-based-on-id/m-p/505473#M135363</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/240711"&gt;@noda6003&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your PROC SORT step is good. Then you could use the LAG and LAG2 functions:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set test;
val=12+3*(pid=lag(pid))+5*(pid=lag2(pid));
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 18 Oct 2018 09:13:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/can-we-randomly-assign-values-to-data-based-on-id/m-p/505473#M135363</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2018-10-18T09:13:32Z</dc:date>
    </item>
    <item>
      <title>Re: can we randomly assign values to data based on id</title>
      <link>https://communities.sas.com/t5/SAS-Programming/can-we-randomly-assign-values-to-data-based-on-id/m-p/505476#M135364</link>
      <description>&lt;P&gt;Do the by-group processing for the correct variable (pid), and use a simple if-then-else:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input pid $ date :ddmmyy10.;
format date ddmmyy10.;
cards;
1      1/1/2011   
1      1/1/2011   
1      1/3/2011 
1      3/4/2011   
2      5/1/2010  
2      6/3/2010
2      6/4/2010
2      12/4/2010
2      14/4/2010
;
run;

data want;
set have;
by pid;
retain val;
if first.pid then val = 12;
else if val = 12 then val = 15;
else val = 20;
run;

proc print data=want noobs;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;pid          date    val

 1     01/01/2011     12
 1     01/01/2011     15
 1     01/03/2011     20
 1     03/04/2011     20
 2     05/01/2010     12
 2     06/03/2010     15
 2     06/04/2010     20
 2     12/04/2010     20
 2     14/04/2010     20
&lt;/PRE&gt;</description>
      <pubDate>Thu, 18 Oct 2018 09:17:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/can-we-randomly-assign-values-to-data-based-on-id/m-p/505476#M135364</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-10-18T09:17:12Z</dc:date>
    </item>
    <item>
      <title>Re: can we randomly assign values to data based on id</title>
      <link>https://communities.sas.com/t5/SAS-Programming/can-we-randomly-assign-values-to-data-based-on-id/m-p/505478#M135365</link>
      <description>&lt;P&gt;Thanks a lot and it worked&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 18 Oct 2018 09:25:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/can-we-randomly-assign-values-to-data-based-on-id/m-p/505478#M135365</guid>
      <dc:creator>noda6003</dc:creator>
      <dc:date>2018-10-18T09:25:34Z</dc:date>
    </item>
  </channel>
</rss>

