<?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 how to assign values based on condition in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/how-to-assign-values-based-on-condition/m-p/512501#M2355</link>
    <description>&lt;P&gt;Dear Community&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I’m looking for a program for the following.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What I have is a table HAVE.&amp;nbsp; It has 3 fields:&amp;nbsp; CD_YEAR, CD_MONTH, var_A. &amp;nbsp;&lt;/P&gt;
&lt;P&gt;This table contains 50 records:&lt;/P&gt;
&lt;P&gt;The first 20 records are all CD_YEAR = 2018 and CD_MONTH = 1&lt;/P&gt;
&lt;P&gt;The next 30 records are all CD_YEAR = 2018 and CD_MONTH = 2&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What I need is a table WANT, where for var_A, &amp;nbsp;a value is filled in following a statistical distribution.&lt;/P&gt;
&lt;P&gt;I’m simplifying here:&amp;nbsp; let’s say the distribution is:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;50% =&amp;gt; var_A = 10&lt;/P&gt;
&lt;P&gt;25% =&amp;gt; var_A = 15&lt;/P&gt;
&lt;P&gt;25% =&amp;gt; var_A = 20&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So in table WANT, the needed result is:&lt;/P&gt;
&lt;P&gt;First 10 records (cd_year= 2018, cd_month = 1) =&amp;gt; var_A = 10&lt;/P&gt;
&lt;P&gt;Next 5 records (cd_year= 2018, cd_month = 1) =&amp;gt; var_A = 15&lt;/P&gt;
&lt;P&gt;Next 5 records (cd_year= 2018, cd_month = 1) =&amp;gt; var_A = 20&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Next 15 records (cd_year= 2018, cd_month = 2) =&amp;gt; var_A = 10&lt;/P&gt;
&lt;P&gt;Next 7 or 8 records (cd_year= 2018, cd_month = 2) =&amp;gt; var_A = 15&lt;/P&gt;
&lt;P&gt;Next 7 or 8 records (cd_year= 2018, cd_month = 2) =&amp;gt; var_A = 20&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Notice the last 2 lines here above:&amp;nbsp; as 25% of 30 records is not a whole number (7,5), it should take 7 or 8 records for var_A = 15.&amp;nbsp; By consequence, it should take resp. 8 or 7 records for var_A = 20.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I hope I explained it clearly enough so that anyone could understand what I want.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I can’t see a method how to implement this.&amp;nbsp; &lt;/P&gt;
&lt;P&gt;Any hints or directions how to deal with this would be very appreciated.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;(the real distribution is a little more complicated, with 13 percentages - I used the above example just to illustrate)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 13 Nov 2018 10:45:53 GMT</pubDate>
    <dc:creator>fre</dc:creator>
    <dc:date>2018-11-13T10:45:53Z</dc:date>
    <item>
      <title>how to assign values based on condition</title>
      <link>https://communities.sas.com/t5/New-SAS-User/how-to-assign-values-based-on-condition/m-p/512501#M2355</link>
      <description>&lt;P&gt;Dear Community&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I’m looking for a program for the following.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What I have is a table HAVE.&amp;nbsp; It has 3 fields:&amp;nbsp; CD_YEAR, CD_MONTH, var_A. &amp;nbsp;&lt;/P&gt;
&lt;P&gt;This table contains 50 records:&lt;/P&gt;
&lt;P&gt;The first 20 records are all CD_YEAR = 2018 and CD_MONTH = 1&lt;/P&gt;
&lt;P&gt;The next 30 records are all CD_YEAR = 2018 and CD_MONTH = 2&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What I need is a table WANT, where for var_A, &amp;nbsp;a value is filled in following a statistical distribution.&lt;/P&gt;
&lt;P&gt;I’m simplifying here:&amp;nbsp; let’s say the distribution is:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;50% =&amp;gt; var_A = 10&lt;/P&gt;
&lt;P&gt;25% =&amp;gt; var_A = 15&lt;/P&gt;
&lt;P&gt;25% =&amp;gt; var_A = 20&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So in table WANT, the needed result is:&lt;/P&gt;
&lt;P&gt;First 10 records (cd_year= 2018, cd_month = 1) =&amp;gt; var_A = 10&lt;/P&gt;
&lt;P&gt;Next 5 records (cd_year= 2018, cd_month = 1) =&amp;gt; var_A = 15&lt;/P&gt;
&lt;P&gt;Next 5 records (cd_year= 2018, cd_month = 1) =&amp;gt; var_A = 20&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Next 15 records (cd_year= 2018, cd_month = 2) =&amp;gt; var_A = 10&lt;/P&gt;
&lt;P&gt;Next 7 or 8 records (cd_year= 2018, cd_month = 2) =&amp;gt; var_A = 15&lt;/P&gt;
&lt;P&gt;Next 7 or 8 records (cd_year= 2018, cd_month = 2) =&amp;gt; var_A = 20&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Notice the last 2 lines here above:&amp;nbsp; as 25% of 30 records is not a whole number (7,5), it should take 7 or 8 records for var_A = 15.&amp;nbsp; By consequence, it should take resp. 8 or 7 records for var_A = 20.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I hope I explained it clearly enough so that anyone could understand what I want.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I can’t see a method how to implement this.&amp;nbsp; &lt;/P&gt;
&lt;P&gt;Any hints or directions how to deal with this would be very appreciated.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;(the real distribution is a little more complicated, with 13 percentages - I used the above example just to illustrate)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 13 Nov 2018 10:45:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/how-to-assign-values-based-on-condition/m-p/512501#M2355</guid>
      <dc:creator>fre</dc:creator>
      <dc:date>2018-11-13T10:45:53Z</dc:date>
    </item>
    <item>
      <title>Re: how to assign values based on condition</title>
      <link>https://communities.sas.com/t5/New-SAS-User/how-to-assign-values-based-on-condition/m-p/512511#M2360</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/28934"&gt;@fre&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This creates your dataset HAVE (without var_A, for which you haven't provided values).&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
cd_year=2018; /* In "example wanted.xlsx" it's 2016. */
do _n_=1 to 50;
  cd_month=1+(_n_&amp;gt;20);
  output;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I'd recommend that you store your "statistical distribution" in a dataset like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data dist;
input prop val;
cards;
0.5  10
0.25 15
0.25 20
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then you can create dataset WANT including variable var_A (or value_A, as it is called in "example wanted.xlsx") as follows:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Create format for cumulative proportions */

data cpfmt(drop=prop);
retain fmtname 'cumprop' sexcl 'N';
set dist(rename=(val=label));
end+prop;
output;
sexcl='Y';
start+prop;
run;

proc format cntlin=cpfmt;
run;

/* Add variable var_A */

data want(drop=_:);
do until(last.cd_month);
  set have;
  by cd_year cd_month;
  _denom=sum(_denom,1);
end;
do until(last.cd_month);
  set have;
  by cd_year cd_month;
  _num=sum(_num,1);
  var_A=input(put(_num/_denom,cumprop.),8.);
  output;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Edit 1: minor simplification in definition of var_A.&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Edit 2: Changed BY statements to&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;by &lt;STRONG&gt;cd_year&lt;/STRONG&gt; cd_month;&lt;/PRE&gt;
&lt;P&gt;in order to allow HAVE datasets containing more than one value of &lt;FONT face="courier new,courier"&gt;cd_year&lt;/FONT&gt; (sorted by &lt;FONT face="courier new,courier"&gt;cd_year cd_month&lt;/FONT&gt;).&lt;/P&gt;</description>
      <pubDate>Tue, 13 Nov 2018 13:55:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/how-to-assign-values-based-on-condition/m-p/512511#M2360</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2018-11-13T13:55:35Z</dc:date>
    </item>
    <item>
      <title>Re: how to assign values based on condition</title>
      <link>https://communities.sas.com/t5/New-SAS-User/how-to-assign-values-based-on-condition/m-p/512521#M2362</link>
      <description>&lt;P&gt;Hi FreelanceReinhard,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;thank you a million times for your code.&amp;nbsp; It's far above my skilsl, but I think I can adapt it for my use.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In the example, year and month are already sorted (ascending) in the first place.&lt;/P&gt;
&lt;P&gt;If I would start from an unsorted dataset, should I first sort it (proc sort), or does this code works without sorting?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;EDIT: I've managed to apply your code to my original dataset.&amp;nbsp; Such a beautiful and efficient code.&amp;nbsp; I've learned a lot today.&amp;nbsp; I'm very thankful to this Community such free support exists, and of course in particular to you, Reinhard!&lt;/P&gt;</description>
      <pubDate>Tue, 13 Nov 2018 14:05:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/how-to-assign-values-based-on-condition/m-p/512521#M2362</guid>
      <dc:creator>fre</dc:creator>
      <dc:date>2018-11-13T14:05:38Z</dc:date>
    </item>
    <item>
      <title>Re: how to assign values based on condition</title>
      <link>https://communities.sas.com/t5/New-SAS-User/how-to-assign-values-based-on-condition/m-p/512536#M2366</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/28934"&gt;@fre&lt;/a&gt;, you're welcome. Yes, the HAVE dataset must be sorted (or indexed) by &lt;FONT face="courier new,courier"&gt;cd_year cd_month&lt;/FONT&gt;, because the final DATA step uses SET in conjunction with BY statements. I have extended the BY statements (see edited code)&amp;nbsp;&lt;SPAN&gt;in order to allow HAVE datasets containing more than one value of&amp;nbsp;&lt;/SPAN&gt;&lt;FONT face="courier new,courier"&gt;cd_year&lt;/FONT&gt;&lt;SPAN&gt;&amp;nbsp;, for example:&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
do cd_year=2016 to 2018;
  do _n_=1 to 50;
    cd_month=1+(_n_&amp;gt;20);
    output;
  end;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;So, if HAVE is not yet sorted, use PROC SORT first:&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=have;
by cd_year cd_month;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/28934"&gt;@fre&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;It's far above my skill, but I think I can adapt it for my use.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;This sounds a bit risky. Please don't hesitate to ask if you have further questions. I'll be happy to check your adaptations if you like.&lt;/P&gt;</description>
      <pubDate>Tue, 13 Nov 2018 14:06:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/how-to-assign-values-based-on-condition/m-p/512536#M2366</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2018-11-13T14:06:29Z</dc:date>
    </item>
  </channel>
</rss>

