<?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: How to find if variable value changed and its type of change in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-if-variable-value-changed-and-its-type-of-change/m-p/692504#M210990</link>
    <description>&lt;P&gt;So how do you define 'permanent' ? If it is the second last product ,it should be permanent ,otherwise 'temporary' ?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input date $	Product $;
cards;
jan	a
feb	a
mar	a
apr	b
may	b
june	b
jul	b
aug	b
sep	b
ovt	b
nov	b
dec	b
;
data temp;
do until(last.product);
 set have;
 by product notsorted;
 if first.product then date_start=date;
end;
date_end=date;
keep product date_start date_end ;
run;
data temp; 
 set temp nobs=nobs;
 n=nobs;
run;
data want;
 merge temp temp(firstobs=2 keep=product rename=(product=p));
 from=product;to=p;

 if _n_=n-1  then change='permanent';
  else  change='temporary ';

 if _n_ ne n then output;
drop product p n;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Mon, 19 Oct 2020 12:56:07 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2020-10-19T12:56:07Z</dc:date>
    <item>
      <title>How to find if variable value changed and its type of change</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-if-variable-value-changed-and-its-type-of-change/m-p/692334#M210898</link>
      <description>&lt;P&gt;Hi everyone,&amp;nbsp;&lt;/P&gt;&lt;P&gt;hope you are well and doing good.&lt;BR /&gt;&lt;BR /&gt;i have one data set like below.&lt;/P&gt;&lt;TABLE border="0" cellspacing="0" cellpadding="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;date&lt;/TD&gt;&lt;TD&gt;Product&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;jan&lt;/TD&gt;&lt;TD&gt;a&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;feb&lt;/TD&gt;&lt;TD&gt;a&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;mar&lt;/TD&gt;&lt;TD&gt;a&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;apr&lt;/TD&gt;&lt;TD&gt;b&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;may&lt;/TD&gt;&lt;TD&gt;b&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;june&lt;/TD&gt;&lt;TD&gt;c&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;jul&lt;/TD&gt;&lt;TD&gt;c&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;aug&lt;/TD&gt;&lt;TD&gt;c&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;sep&lt;/TD&gt;&lt;TD&gt;c&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;ovt&lt;/TD&gt;&lt;TD&gt;b&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;nov&lt;/TD&gt;&lt;TD&gt;b&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;dec&lt;/TD&gt;&lt;TD&gt;b&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;and output for this data step would be like below. Basically i want to check if product was change temporary or permanent.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;from&lt;/TD&gt;&lt;TD&gt;to&lt;/TD&gt;&lt;TD&gt;date_start&lt;/TD&gt;&lt;TD&gt;date_end&lt;/TD&gt;&lt;TD&gt;change&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;a&lt;/TD&gt;&lt;TD&gt;b&lt;/TD&gt;&lt;TD&gt;jan&lt;/TD&gt;&lt;TD&gt;mar&lt;/TD&gt;&lt;TD&gt;temporary&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;b&lt;/TD&gt;&lt;TD&gt;c&lt;/TD&gt;&lt;TD&gt;apr&lt;/TD&gt;&lt;TD&gt;may&lt;/TD&gt;&lt;TD&gt;temporary&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;c&lt;/TD&gt;&lt;TD&gt;b&lt;/TD&gt;&lt;TD&gt;jul&lt;/TD&gt;&lt;TD&gt;sep&lt;/TD&gt;&lt;TD&gt;permanent&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;Please suggest on this how to do this. ?&lt;/P&gt;</description>
      <pubDate>Sun, 18 Oct 2020 08:20:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-if-variable-value-changed-and-its-type-of-change/m-p/692334#M210898</guid>
      <dc:creator>Ashwani_Gupta</dc:creator>
      <dc:date>2020-10-18T08:20:08Z</dc:date>
    </item>
    <item>
      <title>Re: How to find if variable value changed and its type of change</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-if-variable-value-changed-and-its-type-of-change/m-p/692341#M210902</link>
      <description>&lt;P&gt;Assuming I understood what you mean.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input date $	Product $;
cards;
jan	a
feb	a
mar	a
apr	b
may	b
june	c
jul	c
aug	c
sep	c
ovt	b
nov	b
dec	b
;
data temp;
do until(last.product);
 set have;
 by product notsorted;
 if first.product then date_start=date;
end;
date_end=date;
keep product date_start date_end;
run;
data want;
 merge temp temp(firstobs=2 keep=product rename=(product=p));
 from=product;to=p;
 if _n_=1 then do;
  declare hash h();
  h.definekey('p');
  h.definedone();

  declare hash hh();
  hh.definekey('product');
  hh.definedone();
 end;

 if h.check() ne 0 then do;change='temporary ';h.add();end;
  else change='permanent';

 if hh.check() ne 0 then do; hh.add();output;end;
drop product p;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 18 Oct 2020 11:09:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-if-variable-value-changed-and-its-type-of-change/m-p/692341#M210902</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2020-10-18T11:09:24Z</dc:date>
    </item>
    <item>
      <title>Re: How to find if variable value changed and its type of change</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-if-variable-value-changed-and-its-type-of-change/m-p/692345#M210904</link>
      <description>&lt;P&gt;Hi Ksharp,&lt;BR /&gt;Thanks for your response ,&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;This code handle the case right, but if there is change in data like below it will not work,&lt;/P&gt;&lt;TABLE border="0" cellspacing="0" cellpadding="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;date&lt;/TD&gt;&lt;TD&gt;Product&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;jan&lt;/TD&gt;&lt;TD&gt;a&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;feb&lt;/TD&gt;&lt;TD&gt;a&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;mar&lt;/TD&gt;&lt;TD&gt;a&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;apr&lt;/TD&gt;&lt;TD&gt;b&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;may&lt;/TD&gt;&lt;TD&gt;b&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;june&lt;/TD&gt;&lt;TD&gt;b&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;jul&lt;/TD&gt;&lt;TD&gt;b&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;aug&lt;/TD&gt;&lt;TD&gt;b&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;sep&lt;/TD&gt;&lt;TD&gt;b&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;ovt&lt;/TD&gt;&lt;TD&gt;b&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;nov&lt;/TD&gt;&lt;TD&gt;b&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;dec&lt;/TD&gt;&lt;TD&gt;b&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;then it should give only output like below.&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;from&lt;/TD&gt;&lt;TD&gt;to&lt;/TD&gt;&lt;TD&gt;date_start&lt;/TD&gt;&lt;TD&gt;date_end&lt;/TD&gt;&lt;TD&gt;change&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;a&lt;/TD&gt;&lt;TD&gt;b&lt;/TD&gt;&lt;TD&gt;jan&lt;/TD&gt;&lt;TD&gt;mar&lt;/TD&gt;&lt;TD&gt;permanent&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;with your code we will not achive this.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 18 Oct 2020 12:07:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-if-variable-value-changed-and-its-type-of-change/m-p/692345#M210904</guid>
      <dc:creator>Ashwani_Gupta</dc:creator>
      <dc:date>2020-10-18T12:07:09Z</dc:date>
    </item>
    <item>
      <title>Re: How to find if variable value changed and its type of change</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-if-variable-value-changed-and-its-type-of-change/m-p/692354#M210905</link>
      <description>&lt;P&gt;Please define, in plain language, what constitues a permanent or temporary change.&lt;/P&gt;</description>
      <pubDate>Sun, 18 Oct 2020 12:14:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-if-variable-value-changed-and-its-type-of-change/m-p/692354#M210905</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-10-18T12:14:20Z</dc:date>
    </item>
    <item>
      <title>Re: How to find if variable value changed and its type of change</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-if-variable-value-changed-and-its-type-of-change/m-p/692355#M210906</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/352947"&gt;@Ashwani_Gupta&lt;/a&gt;&amp;nbsp;and welcome to the SAS Support Communities!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Try this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Create test data */

data have;
date='01JAN19'd;
do _n_=1 to 12;
  product=byte(97+ranpoi(990067878,1));
  output;
  date=intnx('month',date,1);
end;
format date monname3.;
run;

/* Determine product changes */

data want(drop=_: date product);
do until(last1);
  set have end=last1;
  by product notsorted;
  if first.product then _lfdate=date;
end;
do until(last2);
  set have end=last2 curobs=_n_;
  by product notsorted;
  retain _f 1 from to date_start date_end;
  if _n_=1 then do;
    from=product;
    date_start=date;
  end;
  else if first.product then do;
    to=product;
    length change $9;
    change=ifc(date=_lfdate,'permanent','temporary');
    output;
    _f=0;
    from=to;
    date_start=date;
  end;
  if last.product then date_end=date;
end;
if _f then do;
  to=from;
  change='none';
  output;
end;
format date_: monname3.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Also try different input data, e.g., where no change occurs or frequent changes like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input date product $;
format date monname3.;
cards;
1  a
40  b
80  a
120 b
150 a
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;(Edit: Changed last date value in second test dataset from 160 to 150 to avoid a gap. -- The suggested solution assumes consecutive months.)&lt;/P&gt;</description>
      <pubDate>Sun, 18 Oct 2020 12:41:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-if-variable-value-changed-and-its-type-of-change/m-p/692355#M210906</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2020-10-18T12:41:47Z</dc:date>
    </item>
    <item>
      <title>Re: How to find if variable value changed and its type of change</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-if-variable-value-changed-and-its-type-of-change/m-p/692504#M210990</link>
      <description>&lt;P&gt;So how do you define 'permanent' ? If it is the second last product ,it should be permanent ,otherwise 'temporary' ?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input date $	Product $;
cards;
jan	a
feb	a
mar	a
apr	b
may	b
june	b
jul	b
aug	b
sep	b
ovt	b
nov	b
dec	b
;
data temp;
do until(last.product);
 set have;
 by product notsorted;
 if first.product then date_start=date;
end;
date_end=date;
keep product date_start date_end ;
run;
data temp; 
 set temp nobs=nobs;
 n=nobs;
run;
data want;
 merge temp temp(firstobs=2 keep=product rename=(product=p));
 from=product;to=p;

 if _n_=n-1  then change='permanent';
  else  change='temporary ';

 if _n_ ne n then output;
drop product p n;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 19 Oct 2020 12:56:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-if-variable-value-changed-and-its-type-of-change/m-p/692504#M210990</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2020-10-19T12:56:07Z</dc:date>
    </item>
  </channel>
</rss>

