<?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: Need help with logic in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Need-help-with-logic/m-p/54092#M11432</link>
    <description>Hi Ksharp&lt;BR /&gt;
&lt;BR /&gt;
You'll find the hash object well documented in the SAS Online doc. There is also a bunch of good papers around. &lt;BR /&gt;
&lt;BR /&gt;
Just "googling" fast with keywords "SAS hash table" I found for example this link: &lt;A href="http://www2.sas.com/proceedings/sugi30/236-30.pdf" target="_blank"&gt;http://www2.sas.com/proceedings/sugi30/236-30.pdf&lt;/A&gt;&lt;BR /&gt;
Considering the authors this paper is sure very worthwhile reading.&lt;BR /&gt;
&lt;BR /&gt;
There have been some improvements to the hash object in SAS 9.2. But to get started the SAS 9.1 functionality is very o.k.&lt;BR /&gt;
&lt;BR /&gt;
HTH&lt;BR /&gt;
Patrick</description>
    <pubDate>Thu, 22 Jul 2010 21:54:22 GMT</pubDate>
    <dc:creator>Patrick</dc:creator>
    <dc:date>2010-07-22T21:54:22Z</dc:date>
    <item>
      <title>Need help with logic</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-help-with-logic/m-p/54086#M11426</link>
      <description>Guys, &lt;BR /&gt;
Need some help with a logic that i am trying to create.&lt;BR /&gt;
&lt;BR /&gt;
The data that i am trying to work on is as below&lt;BR /&gt;
&lt;BR /&gt;
data test;&lt;BR /&gt;
infile datalines delimiter = '|';&lt;BR /&gt;
input key: $5. order: 8. stage: $5. field_required : 8.;&lt;BR /&gt;
datalines;&lt;BR /&gt;
AA|1|A|1&lt;BR /&gt;
AA|2|B|1&lt;BR /&gt;
AA|3|C|1&lt;BR /&gt;
AA|4|B|2&lt;BR /&gt;
AA|5|D|2&lt;BR /&gt;
AA|6|C|2&lt;BR /&gt;
AA|7|D|3&lt;BR /&gt;
AA|8|E|3&lt;BR /&gt;
AB|9|B|1&lt;BR /&gt;
AB|10|C|1&lt;BR /&gt;
AB|11|D|1&lt;BR /&gt;
AB|12|C|2&lt;BR /&gt;
AB|13|D|2&lt;BR /&gt;
AB|14|C|3&lt;BR /&gt;
AB|15|D|3&lt;BR /&gt;
;&lt;BR /&gt;
RUN;&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
I am trying to derive the &lt;B&gt;Field_required&lt;/B&gt; column . My logic is as follows.&lt;BR /&gt;
&lt;BR /&gt;
I wish to find out the flow of my &lt;U&gt;stage&lt;/U&gt; variable for each&lt;U&gt; key&lt;/U&gt;. &lt;BR /&gt;
In the key AA the first 3 stage A,B,C are distinct hence they get the value 1,&lt;BR /&gt;
 next the stage B repeats hence it get the  Value 2 .&lt;BR /&gt;
Now whatever follows B will get the value 2 until a repetition of stage  is found under the value 2 .  EX: Stage D occurs in value 2 hence the New stage D gets the value 3 and so on.&lt;BR /&gt;
&lt;BR /&gt;
Note: In the Table you would see that the stage C (i.e order 7) gets a value 2 even though it is present in Value 1 this is because we only look at repetitions in the current value and not in previous values&lt;BR /&gt;
&lt;BR /&gt;
Note: the ORDER variable would always be distinct.&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
I did try using the below macro to solve this. But i am sure that there is a simpler  way of doing this.&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
If someone could Please Guide&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
&lt;I&gt;%macro test(case);&lt;BR /&gt;
proc sql;&lt;BR /&gt;
select count(distinct order) into : cnt&lt;BR /&gt;
from test&lt;BR /&gt;
where KEY = "&amp;amp;case";&lt;BR /&gt;
run;&lt;BR /&gt;
%let cnt = %cmpres(&amp;amp;cnt);&lt;BR /&gt;
proc sql;&lt;BR /&gt;
select distinct order, stage&lt;BR /&gt;
into : st1 -: st&amp;amp;cnt,&lt;BR /&gt;
       : cur1 -: cur&amp;amp;cnt&lt;BR /&gt;
from test &lt;BR /&gt;
where key= "&amp;amp;case"&lt;BR /&gt;
order by order;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
%do i = 1 %to &amp;amp;cnt;&lt;BR /&gt;
	%if &amp;amp;i = 1 %then %do;&lt;BR /&gt;
		data test1;&lt;BR /&gt;
		set test;&lt;BR /&gt;
		format id 8.;&lt;BR /&gt;
		id = 1;&lt;BR /&gt;
		where key = "&amp;amp;CASE" AND order= &amp;amp;&amp;amp;st&amp;amp;i;&lt;BR /&gt;
		run;&lt;BR /&gt;
		%let iid = 1;&lt;BR /&gt;
	%end;&lt;BR /&gt;
	&lt;BR /&gt;
	%if &amp;amp;i &amp;gt;1 %then %do;&lt;BR /&gt;
		proc sql noprint;&lt;BR /&gt;
		select count(stage) &lt;BR /&gt;
		into : check&lt;BR /&gt;
		from test1&lt;BR /&gt;
		where key = "&amp;amp;CASE" AND stage = "&amp;amp;&amp;amp;cur&amp;amp;i" and id = &amp;amp;iid;&lt;BR /&gt;
		run;&lt;BR /&gt;
			%if &amp;amp;check &amp;gt; 0 %then %do;&lt;BR /&gt;
				%let iid = %cmpres(&amp;amp;iid + 1);&lt;BR /&gt;
			%end;&lt;BR /&gt;
		data test2;&lt;BR /&gt;
		set test;&lt;BR /&gt;
		id = &amp;amp;iid;&lt;BR /&gt;
		where order = &amp;amp;&amp;amp;st&amp;amp;i;&lt;BR /&gt;
		run;&lt;BR /&gt;
		data test1;&lt;BR /&gt;
		set test1 test2;&lt;BR /&gt;
		run;&lt;BR /&gt;
	%end;&lt;BR /&gt;
%end;&lt;BR /&gt;
&lt;BR /&gt;
%mend;&lt;BR /&gt;
&lt;BR /&gt;
%macro case_check;&lt;BR /&gt;
PROC SQL NOPRINT;&lt;BR /&gt;
SELECT COUNT(DISTINCT key)&lt;BR /&gt;
INTO : CNTT&lt;BR /&gt;
FROM TEST&lt;BR /&gt;
RUN;&lt;BR /&gt;
%LET CNTT = %CMPRES(&amp;amp;CNTT);&lt;BR /&gt;
PROC SQL NOPRINT;&lt;BR /&gt;
SELECT DISTINCT key &lt;BR /&gt;
INTO : CAS1 -: CAS&amp;amp;CNTT&lt;BR /&gt;
FROM TEST&lt;BR /&gt;
;&lt;BR /&gt;
RUN;&lt;BR /&gt;
&lt;BR /&gt;
%DO J = 1 %TO &amp;amp;CNTT;&lt;BR /&gt;
%test(CASE = &amp;amp;&amp;amp;CAS&amp;amp;J)&lt;BR /&gt;
	%if j = 1 %then %do;&lt;BR /&gt;
	DATA final;&lt;BR /&gt;
	SET TEST1;&lt;BR /&gt;
	RUN;&lt;BR /&gt;
	%end;&lt;BR /&gt;
	%if j&amp;gt;1 %then %do;&lt;BR /&gt;
	DATA final;&lt;BR /&gt;
	SET final TEST1;&lt;BR /&gt;
	RUN;&lt;BR /&gt;
	&lt;BR /&gt;
&lt;BR /&gt;
	%END;&lt;BR /&gt;
%END;&lt;BR /&gt;
%MEND;&lt;BR /&gt;
%CASE_CHECK&lt;/I&gt;</description>
      <pubDate>Thu, 22 Jul 2010 09:15:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-help-with-logic/m-p/54086#M11426</guid>
      <dc:creator>NN</dc:creator>
      <dc:date>2010-07-22T09:15:29Z</dc:date>
    </item>
    <item>
      <title>Re: Need help with logic</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-help-with-logic/m-p/54087#M11427</link>
      <description>Given your reference to "unique" and "repeat", review the topic BY GROUP PROCESSING within a DATA step (on a sorted input file).  You will want to consider how to use IF FIRST.&lt;BYVAR&gt; and/or LAST.&lt;BYVAR&gt;  to identify unique or repeat occurrences within your data, which is combined with a BY statement.  Honestly, I have no clue why you embarked on the MACRO route to start....&lt;BR /&gt;
&lt;BR /&gt;
Also, search the archives as we as SAS.COM on the topic for code examples and explanations -- this topic has been discussed before.&lt;BR /&gt;
&lt;BR /&gt;
And, for self-initiated DATA step debugging, look at using PUTLOG _ALL_;  where you will see the "1" and "0" conditions being set by SAS for the FIRST/LAST conditions when you code the BY statement with your "key" variables.&lt;BR /&gt;
&lt;BR /&gt;
Scott Barry&lt;BR /&gt;
SBBWorks, Inc.&lt;BR /&gt;
&lt;BR /&gt;
Google advanced argument, this topic / post:&lt;BR /&gt;
&lt;BR /&gt;
by group processing site:sas.com

Message was edited by: sbb&lt;/BYVAR&gt;&lt;/BYVAR&gt;</description>
      <pubDate>Thu, 22 Jul 2010 12:43:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-help-with-logic/m-p/54087#M11427</guid>
      <dc:creator>sbb</dc:creator>
      <dc:date>2010-07-22T12:43:07Z</dc:date>
    </item>
    <item>
      <title>Re: Need help with logic</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-help-with-logic/m-p/54088#M11428</link>
      <description>Hi NN &lt;BR /&gt;
&lt;BR /&gt;
Another approach would be to use a dynamically built hash table.&lt;BR /&gt;
&lt;BR /&gt;
You start with an empty hash table.&lt;BR /&gt;
&lt;BR /&gt;
In each iteration of the data step you check if the value of "stage" also exists in the hash table. If the value of "stage" is not there then you add it to the hash table (first occurence), if the value of "stage" is already in the hash table then you add 1 to your fiel_required variable - and you empty the hash table by re-creating it.&lt;BR /&gt;
&lt;BR /&gt;
Also if the key changes in your base data set you re-create the hash table and set the value for field_required to 1.&lt;BR /&gt;
&lt;BR /&gt;
See code below:&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
data test;&lt;BR /&gt;
infile datalines delimiter = '|';&lt;BR /&gt;
input key: $5. order: 8. stage: $5. field_required : 8.;&lt;BR /&gt;
datalines;&lt;BR /&gt;
AA|1|A|1&lt;BR /&gt;
AA|2|B|1&lt;BR /&gt;
AA|3|C|1&lt;BR /&gt;
AA|4|B|2&lt;BR /&gt;
AA|5|D|2&lt;BR /&gt;
AA|6|C|2&lt;BR /&gt;
AA|7|D|3&lt;BR /&gt;
AA|8|E|3&lt;BR /&gt;
AB|9|B|1&lt;BR /&gt;
AB|10|C|1&lt;BR /&gt;
AB|11|D|1&lt;BR /&gt;
AB|12|C|2&lt;BR /&gt;
AB|13|D|2&lt;BR /&gt;
AB|14|C|3&lt;BR /&gt;
AB|15|D|3&lt;BR /&gt;
;&lt;BR /&gt;
RUN;&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
data want;&lt;BR /&gt;
  set test;&lt;BR /&gt;
  by key;&lt;BR /&gt;
  if first.key then field_required_2=1;&lt;BR /&gt;
&lt;BR /&gt;
  if first.key or lag(field_required_2) ne field_required_2 then do;&lt;BR /&gt;
     declare hash h ();&lt;BR /&gt;
     rc = h.defineKey('stage');&lt;BR /&gt;
     rc = h.defineDone();&lt;BR /&gt;
  end;&lt;BR /&gt;
&lt;BR /&gt;
  if (h.check() ne 0) then&lt;BR /&gt;
  do;&lt;BR /&gt;
    h.add();&lt;BR /&gt;
  end;&lt;BR /&gt;
  else&lt;BR /&gt;
  do;&lt;BR /&gt;
    field_required_2+1;&lt;BR /&gt;
  end;&lt;BR /&gt;
&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
proc print data=want;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
The one thing I'm not sure about:&lt;BR /&gt;
When executing a second declare statement with the same hash name does this destroy the previous hash or only disconnect the link to the first hash in memory.&lt;BR /&gt;
If it only unlinks then memory will get more and more clogged with "zombies". &lt;BR /&gt;
&lt;BR /&gt;
This shouldn't be a big issue with the code above if you're not processing millions of records as there is only one key loaded into the hash.&lt;BR /&gt;
&lt;BR /&gt;
Does anyone know the answer? Is there some kind of a destroy method (haven't found it)?&lt;BR /&gt;
&lt;BR /&gt;
Thanks&lt;BR /&gt;
Patrick</description>
      <pubDate>Thu, 22 Jul 2010 13:24:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-help-with-logic/m-p/54088#M11428</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2010-07-22T13:24:41Z</dc:date>
    </item>
    <item>
      <title>Re: Need help with logic</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-help-with-logic/m-p/54089#M11429</link>
      <description>Hi Patrick,&lt;BR /&gt;
Your use of hash definitely looks as per my need. But i have never used hash hence i would have to read about it before i use it in my query.&lt;BR /&gt;
&lt;BR /&gt;
A samll point in the logic you have given. In the output for (order = 14) the Field_required and Field_required_2 are different. Maybe you could help correct this.&lt;BR /&gt;
&lt;BR /&gt;
Thanks....&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Scott,&lt;BR /&gt;
Thanks for the suggestion ,&lt;BR /&gt;
 My lack of knowledge of Data step had forced me to use macros for this Job . &lt;BR /&gt;
I am currently looking at By group Processing with Retain to try and solve this. Hope it would work.&lt;BR /&gt;
&lt;BR /&gt;
Thanks ....</description>
      <pubDate>Thu, 22 Jul 2010 14:42:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-help-with-logic/m-p/54089#M11429</guid>
      <dc:creator>NN</dc:creator>
      <dc:date>2010-07-22T14:42:44Z</dc:date>
    </item>
    <item>
      <title>Re: Need help with logic</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-help-with-logic/m-p/54090#M11430</link>
      <description>There will always have a solution in SAS.;-)&lt;BR /&gt;
&lt;B&gt;Assuming variable 'stage' has five levels such as(A B C D E).&lt;/B&gt;&lt;BR /&gt;
&lt;BR /&gt;
Hi. Mr.Patrick .Can you give me some documentation or files or HyperLink about hash table which I am not famillar with,I need to learn it ,just thought it would be really useful.Thx.&lt;BR /&gt;
&lt;BR /&gt;
[pre]&lt;BR /&gt;
data test;&lt;BR /&gt;
infile datalines delimiter = '|';&lt;BR /&gt;
input key : $5. order : 8. stage : $5.;&lt;BR /&gt;
datalines;&lt;BR /&gt;
AA|1|A&lt;BR /&gt;
AA|2|B&lt;BR /&gt;
AA|3|C&lt;BR /&gt;
AA|4|B&lt;BR /&gt;
AA|5|D&lt;BR /&gt;
AA|6|C&lt;BR /&gt;
AA|7|D&lt;BR /&gt;
AA|8|E&lt;BR /&gt;
AB|9|B&lt;BR /&gt;
AB|10|C&lt;BR /&gt;
AB|11|D&lt;BR /&gt;
AB|12|C&lt;BR /&gt;
AB|13|D&lt;BR /&gt;
AB|14|C&lt;BR /&gt;
AB|15|D&lt;BR /&gt;
;&lt;BR /&gt;
RUN;&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
data temp;&lt;BR /&gt;
 set test;&lt;BR /&gt;
 by key;&lt;BR /&gt;
 length join $20.;&lt;BR /&gt;
 retain join  count;&lt;BR /&gt;
 if first.key    then do;&lt;BR /&gt;
                      count=1;&lt;BR /&gt;
					  call missing(join);&lt;BR /&gt;
					  end;&lt;BR /&gt;
 join=cats(join,stage);&lt;BR /&gt;
 do level='A' ,'B', 'C', 'D' ,'E' ;&lt;BR /&gt;
   num=countc(join,level);&lt;BR /&gt;
   if num ge 2 then do;&lt;BR /&gt;
                       count+1;&lt;BR /&gt;
			  join=trim( stage );&lt;BR /&gt;
                            leave;&lt;BR /&gt;
				    end;&lt;BR /&gt;
 end;&lt;BR /&gt;
  keep key order stage count;&lt;BR /&gt;
 run;&lt;BR /&gt;
proc print;run;&lt;BR /&gt;
&lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Ksharp&lt;BR /&gt;
&lt;BR /&gt;
Code is optimized.&lt;BR /&gt;
&lt;BR /&gt;
Message was edited by: Ksharp

Message was edited by: Ksharp</description>
      <pubDate>Thu, 22 Jul 2010 15:37:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-help-with-logic/m-p/54090#M11430</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2010-07-22T15:37:54Z</dc:date>
    </item>
    <item>
      <title>Re: Need help with logic</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-help-with-logic/m-p/54091#M11431</link>
      <description>Hi NN&lt;BR /&gt;
&lt;BR /&gt;
"In the output for (order = 14) the Field_required and Field_required_2 are different."&lt;BR /&gt;
That's because the value of "stage" is "C" as it was already in row 12. According to "your" logic the value of "field_required_2" must be augmented - and this is what the code does.&lt;BR /&gt;
&lt;BR /&gt;
"...hence i would have to read about it before i use it in my query." &lt;BR /&gt;
Yep, that's how all of us improve our skills....&lt;BR /&gt;
&lt;BR /&gt;
HTH&lt;BR /&gt;
Patrick</description>
      <pubDate>Thu, 22 Jul 2010 21:38:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-help-with-logic/m-p/54091#M11431</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2010-07-22T21:38:37Z</dc:date>
    </item>
    <item>
      <title>Re: Need help with logic</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-help-with-logic/m-p/54092#M11432</link>
      <description>Hi Ksharp&lt;BR /&gt;
&lt;BR /&gt;
You'll find the hash object well documented in the SAS Online doc. There is also a bunch of good papers around. &lt;BR /&gt;
&lt;BR /&gt;
Just "googling" fast with keywords "SAS hash table" I found for example this link: &lt;A href="http://www2.sas.com/proceedings/sugi30/236-30.pdf" target="_blank"&gt;http://www2.sas.com/proceedings/sugi30/236-30.pdf&lt;/A&gt;&lt;BR /&gt;
Considering the authors this paper is sure very worthwhile reading.&lt;BR /&gt;
&lt;BR /&gt;
There have been some improvements to the hash object in SAS 9.2. But to get started the SAS 9.1 functionality is very o.k.&lt;BR /&gt;
&lt;BR /&gt;
HTH&lt;BR /&gt;
Patrick</description>
      <pubDate>Thu, 22 Jul 2010 21:54:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-help-with-logic/m-p/54092#M11432</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2010-07-22T21:54:22Z</dc:date>
    </item>
  </channel>
</rss>

