<?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: Arrays - How to compare columns, then output indicator? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Arrays-How-to-compare-columns-then-output-indicator/m-p/563319#M157905</link>
    <description>Thank you so much for spending time for my question. I will definitely try your method.&lt;BR /&gt;&lt;BR /&gt;Just an additional question on array , how to explicitly list the element? Is it Array c[*] $; ? Thanks again!</description>
    <pubDate>Mon, 03 Jun 2019 16:21:10 GMT</pubDate>
    <dc:creator>Gpp</dc:creator>
    <dc:date>2019-06-03T16:21:10Z</dc:date>
    <item>
      <title>Arrays - How to compare columns, then output indicator?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Arrays-How-to-compare-columns-then-output-indicator/m-p/563271#M157884</link>
      <description>&lt;P&gt;Hi all, I am really bad in array, hoping someone can help me to complete the array.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have to compare column1 vs column2, then column2 vs column3... and each comparison I&amp;nbsp;want create a indicator field.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;dataset A is the table that I want to compare.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;dataset B is the incomplete code that I try to write, I know my array c{*} is incorrect but I don't know how to modify it.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Sample output of dataset D,&amp;nbsp;the last 5 columns in numeric&amp;nbsp;are what I want to compute.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="sample.JPG" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/29964i274078FB519720AE/image-size/large?v=v2&amp;amp;px=999" role="button" title="sample.JPG" alt="sample.JPG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;%let mthrun=5;
%let dt=01mar2019;

data _null_;
	do i=1 to &amp;amp;mthrun.;
	mth=put(intnx('month', "&amp;amp;dt"d, i-&amp;amp;mthrun.),yymmn6.);
	call symput ("mth"||compress(i),mth);
	put i mth;
	end;
run;

data A;
input id $2. J_201811 $2.  J_201812 $2.  J_201901 $2.  J_201902 $2.  J_201903 $2.;
DATALINES;
AAQ1Q1Q1Q3T5
BB  R1Q1R1T1
CC    Y1Y1Y1
DDG1G1G1G1G1
;
run;


DATA B;
	SET A;
	ARRAY JC{*} J_:;
	ARRAY C{*} C_J:;
		DO i=1 to dim(JC)-1;
		IF jc{i} NE "" AND jc{i} NE jc{i+1} THEN C_J{i+1}=1;
			else C_J{i+1}=0;
		END; 
RUN;

DATA D;
	SET A;
	IF J_201811 NE '' AND J_201811 NE J_201812 THEN C_J_201812=1; ELSE C_J_201812=0;
	IF J_201812 NE '' AND J_201812 NE J_201901 THEN C_J_201901=1; ELSE C_J_201901=0;
	IF J_201901 NE '' AND J_201901 NE J_201902 THEN C_J_201902=1; ELSE C_J_201902=0;
	IF J_201902 NE '' AND J_201902 NE J_201903 THEN C_J_201903=1; ELSE C_J_201903=0;
	NO_OF_CHANGES=SUM(OF C_J_201812--C_J_201903);
RUN;&lt;/PRE&gt;&lt;P&gt;Thank you in advance &lt;span class="lia-unicode-emoji" title=":grinning_face_with_big_eyes:"&gt;😃&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 03 Jun 2019 14:41:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Arrays-How-to-compare-columns-then-output-indicator/m-p/563271#M157884</guid>
      <dc:creator>Gpp</dc:creator>
      <dc:date>2019-06-03T14:41:45Z</dc:date>
    </item>
    <item>
      <title>Re: Arrays - How to compare columns, then output indicator?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Arrays-How-to-compare-columns-then-output-indicator/m-p/563281#M157888</link>
      <description>&lt;P&gt;It may help if you explain what you're trying to do overall. I would recommend redesigning this approach to work with a long data set and then flip at the end for reporting. It's more dynamic that way and you are less likely to need to hardcode any values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/264087"&gt;@Gpp&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi all, I am really bad in array, hoping someone can help me to complete the array.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have to compare column1 vs column2, then column2 vs column3... and each comparison I&amp;nbsp;want create a indicator field.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;dataset A is the table that I want to compare.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;dataset B is the incomplete code that I try to write, I know my array c{*} is incorrect but I don't know how to modify it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Sample output of dataset D,&amp;nbsp;the last 5 columns in numeric&amp;nbsp;are what I want to compute.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="sample.JPG" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/29964i274078FB519720AE/image-size/large?v=v2&amp;amp;px=999" role="button" title="sample.JPG" alt="sample.JPG" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;%let mthrun=5;
%let dt=01mar2019;

data _null_;
	do i=1 to &amp;amp;mthrun.;
	mth=put(intnx('month', "&amp;amp;dt"d, i-&amp;amp;mthrun.),yymmn6.);
	call symput ("mth"||compress(i),mth);
	put i mth;
	end;
run;

data A;
input id $2. J_201811 $2.  J_201812 $2.  J_201901 $2.  J_201902 $2.  J_201903 $2.;
DATALINES;
AAQ1Q1Q1Q3T5
BB  R1Q1R1T1
CC    Y1Y1Y1
DDG1G1G1G1G1
;
run;


DATA B;
	SET A;
	ARRAY JC{*} J_:;
	ARRAY C{*} C_J:;
		DO i=1 to dim(JC)-1;
		IF jc{i} NE "" AND jc{i} NE jc{i+1} THEN C_J{i+1}=1;
			else C_J{i+1}=0;
		END; 
RUN;

DATA D;
	SET A;
	IF J_201811 NE '' AND J_201811 NE J_201812 THEN C_J_201812=1; ELSE C_J_201812=0;
	IF J_201812 NE '' AND J_201812 NE J_201901 THEN C_J_201901=1; ELSE C_J_201901=0;
	IF J_201901 NE '' AND J_201901 NE J_201902 THEN C_J_201902=1; ELSE C_J_201902=0;
	IF J_201902 NE '' AND J_201902 NE J_201903 THEN C_J_201903=1; ELSE C_J_201903=0;
	NO_OF_CHANGES=SUM(OF C_J_201812--C_J_201903);
RUN;&lt;/PRE&gt;
&lt;P&gt;Thank you in advance &lt;span class="lia-unicode-emoji" title=":grinning_face_with_big_eyes:"&gt;😃&lt;/span&gt;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 03 Jun 2019 15:04:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Arrays-How-to-compare-columns-then-output-indicator/m-p/563281#M157888</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-06-03T15:04:15Z</dc:date>
    </item>
    <item>
      <title>Re: Arrays - How to compare columns, then output indicator?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Arrays-How-to-compare-columns-then-output-indicator/m-p/563289#M157893</link>
      <description>Hello Reza, I am trying to know for the past 2 years (24 column for each month ) any changes between previous month and current month.&lt;BR /&gt;&lt;BR /&gt;Example j_201811=“Q1” and j_201812=“W1” then count=1.&lt;BR /&gt;But if the j_201811 = “” and j_201812 ne “”, then is not consider as a change , so count=0&lt;BR /&gt;j_201811=“Q1” and j_201812=“Q1” then count=0&lt;BR /&gt;&lt;BR /&gt;in the end of the day I want to know , how many times the contents of the column changed.</description>
      <pubDate>Mon, 03 Jun 2019 15:16:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Arrays-How-to-compare-columns-then-output-indicator/m-p/563289#M157893</guid>
      <dc:creator>Gpp</dc:creator>
      <dc:date>2019-06-03T15:16:34Z</dc:date>
    </item>
    <item>
      <title>Re: Arrays - How to compare columns, then output indicator?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Arrays-How-to-compare-columns-then-output-indicator/m-p/563294#M157894</link>
      <description>Your code seems mostly correct to me then, what isn't happening that you want to happen. &lt;BR /&gt;&lt;BR /&gt;I can't run your code because you didn't provide any sample data (image is not data).</description>
      <pubDate>Mon, 03 Jun 2019 15:27:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Arrays-How-to-compare-columns-then-output-indicator/m-p/563294#M157894</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-06-03T15:27:53Z</dc:date>
    </item>
    <item>
      <title>Re: Arrays - How to compare columns, then output indicator?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Arrays-How-to-compare-columns-then-output-indicator/m-p/563297#M157895</link>
      <description>%let mthrun=5;&lt;BR /&gt;%let dt=01mar2019;&lt;BR /&gt;&lt;BR /&gt;data _null_;&lt;BR /&gt;do i=1 to &amp;amp;mthrun.;&lt;BR /&gt;mth=put(intnx('month', "&amp;amp;dt"d, i-&amp;amp;mthrun.),yymmn6.);&lt;BR /&gt;call symput ("mth"||compress(i),mth);&lt;BR /&gt;put i mth;&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;data A;&lt;BR /&gt;input id $2. J_201811 $2. J_201812 $2. J_201901 $2. J_201902 $2. J_201903 $2.;&lt;BR /&gt;DATALINES;&lt;BR /&gt;AAQ1Q1Q1Q3T5&lt;BR /&gt;BB R1Q1R1T1&lt;BR /&gt;CC Y1Y1Y1&lt;BR /&gt;DDG1G1G1G1G1&lt;BR /&gt;;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;DATA B;&lt;BR /&gt;SET A;&lt;BR /&gt;ARRAY JC{*} J_:;&lt;BR /&gt;ARRAY C{*} C_J:;&lt;BR /&gt;DO i=1 to dim(JC)-1;&lt;BR /&gt;IF jc{i} NE "" AND jc{i} NE jc{i+1} THEN C_J{i+1}=1;&lt;BR /&gt;else C_J{i+1}=0;&lt;BR /&gt;END;&lt;BR /&gt;RUN;&lt;BR /&gt;&lt;BR /&gt;DATA D;&lt;BR /&gt;SET A;&lt;BR /&gt;IF J_201811 NE '' AND J_201811 NE J_201812 THEN C_J_201812=1; ELSE C_J_201812=0;&lt;BR /&gt;IF J_201812 NE '' AND J_201812 NE J_201901 THEN C_J_201901=1; ELSE C_J_201901=0;&lt;BR /&gt;IF J_201901 NE '' AND J_201901 NE J_201902 THEN C_J_201902=1; ELSE C_J_201902=0;&lt;BR /&gt;IF J_201902 NE '' AND J_201902 NE J_201903 THEN C_J_201903=1; ELSE C_J_201903=0;&lt;BR /&gt;NO_OF_CHANGES=SUM(OF C_J_201812--C_J_201903);&lt;BR /&gt;RUN;&lt;BR /&gt;&lt;BR /&gt;Can you use this ? Dataset A is my sample data . Dataset b is my desired output&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Mon, 03 Jun 2019 15:31:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Arrays-How-to-compare-columns-then-output-indicator/m-p/563297#M157895</guid>
      <dc:creator>Gpp</dc:creator>
      <dc:date>2019-06-03T15:31:50Z</dc:date>
    </item>
    <item>
      <title>Re: Arrays - How to compare columns, then output indicator?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Arrays-How-to-compare-columns-then-output-indicator/m-p/563301#M157897</link>
      <description>If B is expected output, what's wrong with the code?&lt;BR /&gt;If B is close but not exact, please show an exact output expected. &lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Mon, 03 Jun 2019 15:35:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Arrays-How-to-compare-columns-then-output-indicator/m-p/563301#M157897</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-06-03T15:35:26Z</dc:date>
    </item>
    <item>
      <title>Re: Arrays - How to compare columns, then output indicator?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Arrays-How-to-compare-columns-then-output-indicator/m-p/563305#M157899</link>
      <description>Sorry, is D will generate the output that I want , but D is too lengthy , so I try to use array in B, but B with error. I am not sure how to make B correct</description>
      <pubDate>Mon, 03 Jun 2019 15:40:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Arrays-How-to-compare-columns-then-output-indicator/m-p/563305#M157899</guid>
      <dc:creator>Gpp</dc:creator>
      <dc:date>2019-06-03T15:40:00Z</dc:date>
    </item>
    <item>
      <title>Re: Arrays - How to compare columns, then output indicator?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Arrays-How-to-compare-columns-then-output-indicator/m-p/563313#M157901</link>
      <description>&lt;P&gt;You can't declare an array like this when it doesn't exist. This type of function only works when you have the variables already in the data set:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;ARRAY C{*} C_J:;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So you need to explicitly list the elements out and it will work fine.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That being said, that's why this is not a dynamic solution - it becomes hard coded and requires you modifying it every month or every run. Changing it to a long format doesn't require any of these issues and simplifies your code massively.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 03 Jun 2019 15:55:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Arrays-How-to-compare-columns-then-output-indicator/m-p/563313#M157901</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-06-03T15:55:22Z</dc:date>
    </item>
    <item>
      <title>Re: Arrays - How to compare columns, then output indicator?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Arrays-How-to-compare-columns-then-output-indicator/m-p/563318#M157904</link>
      <description>&lt;P&gt;This is how I would approach this instead:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;

data A;
input id $2. J_201811 $2. J_201812 $2. J_201901 $2. J_201902 $2. J_201903 $2.;
DATALINES;
AAQ1Q1Q1Q3T5
BB R1Q1R1T1
CC Y1Y1Y1
DDG1G1G1G1G1
;
run;

*transpose to a long format;
proc transpose data=A out=long1;
by ID;
VAR J:;
run;

*add in dates and rename columns;
data long2;
set long;
date = input(substr(_name_, 3, 6), yymmn6.);
format date yymmn6.;
rename col1 = VALUE;
drop _name_ ;
run;

*check if matches previous value;
data long3;
set long2;
by ID date;

*gets previous value;

prev_value = lag(value);

*checks if new id and sets to missing if it is a new ID;
if first.id then call missing(prev_value);

*code as 1/0 if the same as previous value;
if not first.id then do;
if prev_value = value then flag=1; else flag=0;
end;

run;

*calculate summary statistics;
proc sql;
create table comparison as
select ID, mean(flag) as Percent_Change format=percent12.1, sum(flag) as N_Changed, n(flag) as N_Records
from long3
group by ID;
quit;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 03 Jun 2019 16:17:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Arrays-How-to-compare-columns-then-output-indicator/m-p/563318#M157904</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-06-03T16:17:44Z</dc:date>
    </item>
    <item>
      <title>Re: Arrays - How to compare columns, then output indicator?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Arrays-How-to-compare-columns-then-output-indicator/m-p/563319#M157905</link>
      <description>Thank you so much for spending time for my question. I will definitely try your method.&lt;BR /&gt;&lt;BR /&gt;Just an additional question on array , how to explicitly list the element? Is it Array c[*] $; ? Thanks again!</description>
      <pubDate>Mon, 03 Jun 2019 16:21:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Arrays-How-to-compare-columns-then-output-indicator/m-p/563319#M157905</guid>
      <dc:creator>Gpp</dc:creator>
      <dc:date>2019-06-03T16:21:10Z</dc:date>
    </item>
    <item>
      <title>Re: Arrays - How to compare columns, then output indicator?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Arrays-How-to-compare-columns-then-output-indicator/m-p/563322#M157907</link>
      <description>&lt;PRE&gt;array c(*) $ C_201811 C_201812 C_201901 ....  C_201912;&lt;/PRE&gt;
&lt;P&gt;You have to list all the elements individually if you need new elements with a different prefix. You could automate that portion with a macro variable or macro as well.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/264087"&gt;@Gpp&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Thank you so much for spending time for my question. I will definitely try your method.&lt;BR /&gt;&lt;BR /&gt;Just an additional question on array , how to explicitly list the element? Is it Array c[*] $; ? Thanks again!&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 03 Jun 2019 16:24:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Arrays-How-to-compare-columns-then-output-indicator/m-p/563322#M157907</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-06-03T16:24:18Z</dc:date>
    </item>
  </channel>
</rss>

