<?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 do i put most common input in a new variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-i-put-the-most-common-input-in-a-new-variable/m-p/600985#M173787</link>
    <description>&lt;P&gt;Thank you. I wanted to delete one, but I couldn't figure out how to..im new here! &amp;gt;&amp;lt;. Thanks again.&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 01 Nov 2019 15:02:05 GMT</pubDate>
    <dc:creator>Diosfreak</dc:creator>
    <dc:date>2019-11-01T15:02:05Z</dc:date>
    <item>
      <title>How to i put the most common input in a new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-i-put-the-most-common-input-in-a-new-variable/m-p/600855#M173746</link>
      <description>&lt;P&gt;My dataset looks like this&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;id&amp;nbsp; &amp;nbsp; &amp;nbsp;gap&amp;nbsp; &amp;nbsp; insurance&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; -5&amp;nbsp; &amp;nbsp; &amp;nbsp; one&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; 2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;one&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp;9&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; two&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; three&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; 10&amp;nbsp; &amp;nbsp; &amp;nbsp; one&amp;nbsp;&lt;/P&gt;&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp;5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;four&lt;/P&gt;&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp;4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;four&lt;/P&gt;&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp;2&amp;nbsp; &amp;nbsp; &amp;nbsp; one&lt;/P&gt;&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp;-3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;two&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What i want to do is the following, i want a new variable created that will give me the most occuring insurance if the gap is between -5 to 5 days.&amp;nbsp;&lt;/P&gt;&lt;P&gt;id-Unique id of the individual&lt;/P&gt;&lt;P&gt;gap-gap in days&lt;/P&gt;&lt;P&gt;insurance-the different types of insurance&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So my final result should look like&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;id&amp;nbsp; &amp;nbsp; &amp;nbsp;gap&amp;nbsp; &amp;nbsp; insurance&amp;nbsp; &amp;nbsp;finalinsurance&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; -5&amp;nbsp; &amp;nbsp; &amp;nbsp; one&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; one&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; 2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;one&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; one&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp;9&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; two&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;one&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; three&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; three&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; 10&amp;nbsp; &amp;nbsp; &amp;nbsp; one&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;three&lt;/P&gt;&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp;5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;four&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; four&lt;/P&gt;&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp;4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;four&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; four&lt;/P&gt;&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp;2&amp;nbsp; &amp;nbsp; &amp;nbsp; one&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;four&lt;/P&gt;&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp;-3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;two&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;four&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;some more insight on the result:&lt;/P&gt;&lt;P&gt;ID 1 has 2 insurance that falls within my -5 to 5 days gap so i take a count of it, and i pick the most common one. In this case it is one so i populate the new variable with this answer.&lt;/P&gt;&lt;P&gt;ID 2 only one met my criteria so i went with the insurance , which is three.&lt;/P&gt;&lt;P&gt;ID 3, they all fall within my gap, so i take a count of the insurance input and go with the most common one, in this case insurance type four is the most common one.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please any assistance will be appreciated. Thank you.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 31 Oct 2019 23:11:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-i-put-the-most-common-input-in-a-new-variable/m-p/600855#M173746</guid>
      <dc:creator>Diosfreak</dc:creator>
      <dc:date>2019-10-31T23:11:52Z</dc:date>
    </item>
    <item>
      <title>How do i put most common input in a new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-i-put-the-most-common-input-in-a-new-variable/m-p/600854#M173764</link>
      <description>&lt;P&gt;My dataset looks like this&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;id&amp;nbsp; &amp;nbsp; &amp;nbsp;gap&amp;nbsp; &amp;nbsp; insurance&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; -5&amp;nbsp; &amp;nbsp; &amp;nbsp; one&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; 2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;one&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp;9&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; two&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; three&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; 10&amp;nbsp; &amp;nbsp; &amp;nbsp; one&amp;nbsp;&lt;/P&gt;&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp;5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;four&lt;/P&gt;&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp;4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;four&lt;/P&gt;&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp;2&amp;nbsp; &amp;nbsp; &amp;nbsp; one&lt;/P&gt;&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp;-3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;two&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What i want to do is the following, i want a new variable created that will give me the most occuring insurance if the gap is between -5 to 5 days.&amp;nbsp;&lt;/P&gt;&lt;P&gt;id-Unique id of the individual&lt;/P&gt;&lt;P&gt;gap-gap in days&lt;/P&gt;&lt;P&gt;insurance-the different types of insurance&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So my final result should look like&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;id&amp;nbsp; &amp;nbsp; &amp;nbsp;gap&amp;nbsp; &amp;nbsp; insurance&amp;nbsp; &amp;nbsp;finalinsurance&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; -5&amp;nbsp; &amp;nbsp; &amp;nbsp; one&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; one&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; 2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;one&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; one&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp;9&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; two&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;one&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; three&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; three&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; 10&amp;nbsp; &amp;nbsp; &amp;nbsp; one&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;three&lt;/P&gt;&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp;5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;four&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; four&lt;/P&gt;&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp;4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;four&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; four&lt;/P&gt;&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp;2&amp;nbsp; &amp;nbsp; &amp;nbsp; one&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;four&lt;/P&gt;&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp;-3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;two&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;four&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;some more insight on the result:&lt;/P&gt;&lt;P&gt;ID 1 has 2 insurance that falls within my -5 to 5 days gap so i take a count of it, and i pick the most common one. In this case it is one so i populate the new variable with this answer.&lt;/P&gt;&lt;P&gt;ID 2 only one met my criteria so i went with the insurance , which is three.&lt;/P&gt;&lt;P&gt;ID 3, they all fall within my gap, so i take a count of the insurance input and go with the most common one, in this case insurance type four is the most common one.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please any assistance will be appreciated. Thank you.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 31 Oct 2019 23:10:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-i-put-the-most-common-input-in-a-new-variable/m-p/600854#M173764</guid>
      <dc:creator>Diosfreak</dc:creator>
      <dc:date>2019-10-31T23:10:49Z</dc:date>
    </item>
    <item>
      <title>Re: How to i put the most common input in a new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-i-put-the-most-common-input-in-a-new-variable/m-p/600861#M173748</link>
      <description>&lt;P&gt;Give this a shot.&lt;/P&gt;&lt;P&gt;1. Read in "raw_data" that you supplied.&lt;/P&gt;&lt;P&gt;2. Create a frequency table on id and insurance- call it "outfreq".&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;(be sure to filter raw_data down to observations with gaps between -5 and 5)&amp;nbsp;&lt;/P&gt;&lt;P&gt;3. Sort raw_data for later merge.&lt;/P&gt;&lt;P&gt;4. Sort "outfreq" so that most frequent by id is on top.&lt;/P&gt;&lt;P&gt;5. De-duplicate "outfreq" - call it "mostfreq".&lt;/P&gt;&lt;P&gt;6. Merge "mostfreq" with raw_data (like a left join).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data raw_data;
input id gap insurance $;
datalines;
1 -5 one
1 2 one
1 9 two
2 1 three
2 10 one 
3 5 four
3 4 four
3 2 one
3 -3 two
;
run;

proc freq data = raw_data(where=(gap ge -5 and gap le 5)) noprint;
tables id*insurance / norow nocol nopercent nocum
out = outfreq(rename=count=finalinsurance drop=percent);
run; 

proc sort data=raw_data; 
	by id; 
run;
proc sort data=outfreq; 
	by id descending finalinsurance; 
run;
proc sort data=outfreq(keep=id insurance) 
	nodupkey 
	out=mostfreq(rename=insurance=finalinsurance); 
	by id; 
run;

data want;
	merge
		raw_data(in=a)
		mostfreq(in=b) 
	;
	by id;
	if a;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;-unison&lt;/P&gt;</description>
      <pubDate>Fri, 01 Nov 2019 00:25:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-i-put-the-most-common-input-in-a-new-variable/m-p/600861#M173748</guid>
      <dc:creator>unison</dc:creator>
      <dc:date>2019-11-01T00:25:26Z</dc:date>
    </item>
    <item>
      <title>Re: How to i put the most common input in a new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-i-put-the-most-common-input-in-a-new-variable/m-p/600862#M173749</link>
      <description>&lt;P&gt;You first need to first get frequencies of INSURANCE, by ID, for records with gap between -5 and 5:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input id     gap    insurance :$5. ;
datalines;
1    -5      one
1     2      one
1     9      two
2     1      three
2    10      one 
3     5      four
3     4      four
3     2      one
3    -3      two
run;

proc summary data=have (where=(gap between -5 and 5)) order=freq noprint nway; 
  by id;
  class insurance ;
  output out=t;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The PROC SUMMARY will generate many requested statistics (just frequencies in this case) of every level of the class variable (insurance).&amp;nbsp; And the "by id" tells SAS to do this separately for each ID (the data are assumed to be sorted by ID).&amp;nbsp; The "order=freq" options tells proc summary not to output (to data set T)&amp;nbsp; INSURANCE frequencies alphabetically, but in descending frequency order (again within each level of the ID variable).&amp;nbsp; The NWAY option says don't output global frequency.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now you need a way to combine this with the original dataset HAVE:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop=_ins);
  retain _ins '       ';
  set t (keep=id insurance)
      have (in=inhave);
  by id;
  if first.id=1 then _ins=insurance;
  else insurnace=_ins;
  if inhave=1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The program, for each ID, reads all the records in T&amp;nbsp; (sorted by descending frequency), followed by all the records in HAVE.&amp;nbsp; The "BY ID" statement also tells SAS to make temporary dummy variables FIRST.ID and LAST.ID telling you whether the record-in-hand is the first record for an ID (i.e. the first, highest-frequency, record in T, has first.id=1).&amp;nbsp; The last.id=1 would be the final record for an ID in HAVE.&amp;nbsp; They are zero otherwise.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So when the first, highest-frequency, record is in hand copy the INSURANCE value to the retained variable _INS.&amp;nbsp; Otherwise copy _INS back to INSURANCE.&amp;nbsp; Finally the subsetting IF&amp;nbsp; (if inhave=1) tells SAS to keep only the observations starting out in HAVE.&lt;/P&gt;</description>
      <pubDate>Fri, 01 Nov 2019 00:26:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-i-put-the-most-common-input-in-a-new-variable/m-p/600862#M173749</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2019-11-01T00:26:39Z</dc:date>
    </item>
    <item>
      <title>Re: How do i put most common input in a new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-i-put-the-most-common-input-in-a-new-variable/m-p/600863#M173765</link>
      <description>&lt;P&gt;I suggest you remove this topic, or mark it as solved, since youi already have posted it in a more appropriate forum group.&lt;/P&gt;</description>
      <pubDate>Fri, 01 Nov 2019 00:28:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-i-put-the-most-common-input-in-a-new-variable/m-p/600863#M173765</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2019-11-01T00:28:32Z</dc:date>
    </item>
    <item>
      <title>Re: How do i put most common input in a new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-i-put-the-most-common-input-in-a-new-variable/m-p/600880#M173766</link>
      <description>&lt;P&gt;Yes thanks! Its my first time using this forum, i was not sure where to paste my question. Thanks again.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 01 Nov 2019 03:26:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-i-put-the-most-common-input-in-a-new-variable/m-p/600880#M173766</guid>
      <dc:creator>Diosfreak</dc:creator>
      <dc:date>2019-11-01T03:26:26Z</dc:date>
    </item>
    <item>
      <title>Re: How do i put most common input in a new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-i-put-the-most-common-input-in-a-new-variable/m-p/600896#M173767</link>
      <description>&lt;P&gt;I merged the two questions.&lt;/P&gt;</description>
      <pubDate>Fri, 01 Nov 2019 08:14:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-i-put-the-most-common-input-in-a-new-variable/m-p/600896#M173767</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-11-01T08:14:28Z</dc:date>
    </item>
    <item>
      <title>Re: How do i put most common input in a new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-i-put-the-most-common-input-in-a-new-variable/m-p/600985#M173787</link>
      <description>&lt;P&gt;Thank you. I wanted to delete one, but I couldn't figure out how to..im new here! &amp;gt;&amp;lt;. Thanks again.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 01 Nov 2019 15:02:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-i-put-the-most-common-input-in-a-new-variable/m-p/600985#M173787</guid>
      <dc:creator>Diosfreak</dc:creator>
      <dc:date>2019-11-01T15:02:05Z</dc:date>
    </item>
    <item>
      <title>Re: How to i put the most common input in a new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-i-put-the-most-common-input-in-a-new-variable/m-p/601032#M173809</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/283027"&gt;@Diosfreak&lt;/a&gt;:&lt;/P&gt;
&lt;P&gt;Using the power of the hash object, this can be done in a single step:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;                                                                                                                             
  input id gap insurance :$5. ;                                                                                                         
  cards ;                                                                                                                               
1    -5      one                                                                                                                        
1     2      one                                                                                                                        
1     9      two                                                                                                                        
2     1      three                                                                                                                      
2    10      one                                                                                                                        
3     5      four                                                                                                                       
3     4      four                                                                                                                       
3     2      one                                                                                                                        
3    -3      two                                                                                                                        
run ;                                                                                                                                   
                                                                                                                                        
data want (drop = _:) ;                                                                                                                 
  if _n_ = 1 then do ;                                                                                                                  
    dcl hash h() ;                                                                                                                      
    dcl hiter i ("h") ;                                                                                                                 
    h.definekey  ("insurance") ;                                                                                                        
    h.definedata ("insurance", "_f") ;                                                                                                  
    h.definedone () ;                                                                                                                   
  end ;                                                                                                                                 
  do _n_ = 1 by 1 until (last.id) ;                                                                                                     
    set have ;                                                                                                                          
    by id ;                                                                                                                             
    if not (-5 &amp;lt;= gap &amp;lt;= 5) then continue ;                                                                                             
    if h.find() ne 0 then _f = 1 ;                                                                                                      
    else                  _f + 1 ;                                                                                                      
    h.replace() ;                                                                                                                       
  end ;                                                                                                                                 
  do while (i.next() = 0) ;                                                                                                             
    if _f &amp;lt;= _fmax then continue ;                                                                                                      
    _fmax = _f ;                                                                                                                        
    finalinsurance = insurance ;                                                                                                        
  end ;                                                                                                                                 
  do _n_ = 1 to _n_ ;                                                                                                                   
    set have ;                                                                                                                          
    output ;                                                                                                                            
  end ;                                                                                                                                 
  h.clear() ;                                                                                                                           
run ;                                     
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;How it works:&lt;/P&gt;
&lt;P&gt;1. The next by-group by ID is read twice.&lt;/P&gt;
&lt;P&gt;2. On the first pass, only the records with gap between -5 and 5 are considered.&lt;/P&gt;
&lt;P&gt;3. If the insurance key-value not found in the hash table H (created beforehand at _N_=1), it is inserted into the table with _F=1. Otherwise, 1 is added to the table value of _F for this key-value. So, by the end of the by group, the table contains the unique values of insurance and corresponding frequencies _F.&lt;/P&gt;
&lt;P&gt;4. After the by-group is this processed, the table is scanned using the hash iterator I to find the value of insurance with the highest frequency assigned to variable finalinsurance.&lt;/P&gt;
&lt;P&gt;5. The same by-group (i.e. with the same ID value) is read again, and each record is written out with the value of finalinsurance attached.&lt;/P&gt;
&lt;P&gt;6. The hash table is cleared of all the items, and the process is repeated from #1.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The program above&amp;nbsp;assumes that the input file is sorted by ID; however it can be rewritten even without this assumption. For those learning programming with the SAS hash object it can be a nice exercise.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;Paul D.&lt;/P&gt;</description>
      <pubDate>Fri, 01 Nov 2019 18:05:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-i-put-the-most-common-input-in-a-new-variable/m-p/601032#M173809</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2019-11-01T18:05:16Z</dc:date>
    </item>
    <item>
      <title>Re: How to i put the most common input in a new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-i-put-the-most-common-input-in-a-new-variable/m-p/602527#M174463</link>
      <description>&lt;P&gt;This worked great! Thank you very much for the prompt solution.&lt;/P&gt;</description>
      <pubDate>Thu, 07 Nov 2019 18:02:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-i-put-the-most-common-input-in-a-new-variable/m-p/602527#M174463</guid>
      <dc:creator>Diosfreak</dc:creator>
      <dc:date>2019-11-07T18:02:54Z</dc:date>
    </item>
    <item>
      <title>Re: How to i put the most common input in a new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-i-put-the-most-common-input-in-a-new-variable/m-p/602529#M174464</link>
      <description>&lt;P&gt;This worked as well. THank you very much.&lt;/P&gt;</description>
      <pubDate>Thu, 07 Nov 2019 18:05:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-i-put-the-most-common-input-in-a-new-variable/m-p/602529#M174464</guid>
      <dc:creator>Diosfreak</dc:creator>
      <dc:date>2019-11-07T18:05:20Z</dc:date>
    </item>
  </channel>
</rss>

