Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: WHILE command, the end goal


From   Vincent Davis <[email protected]>
To   [email protected]
Subject   st: WHILE command, the end goal
Date   Mon, 17 Mar 2008 09:52:48 -0600

Thanks for the info, I am using forvalues and foreach for parts of  
my .do file but I can't seem to find a way to solve my problem without  
using WHILE or maybe solve it at all but thats from lack of knowledge.

If anyone is interested, I am sure there is a better way to do what I  
am trying to do
What I am doing is trying to execute the following algorithm on my data.
http://www.nrmp.org/res_match/about_res/algorithms.html
My data looks as follows.
pnum: program number
anum: applicant number
pra: Program ranks applicant
arp: Applicant ranks program
Other contidions
applicant rank only programs that rank them but not necessarily all  
programs.
not all applicant are ranked
In my setup there are 12500 applicants and 100 programs
Each program ranks 400 applicants
Each program accepts 50
Each applicant ranks 10 Programs (of the programs that ranked them)  
(matches with only one)
Programs may not fill
Applicants may not match even if ranked
The result I am trying to achieve is a variable "matched" that  
indicates that that observation is a "match" matched = 1 if yes or  
matched = 0 if no
So I have something that looks like:

pnum	anum	pra		arp
1		30		5		9
1		998		.		.
2		350		123		.
3		350		3		1
4		350		45		2	

Currently I am trying to piece it together and I have this. What I  
think are my are
Testing if I am done. Applicant is matched or all applicant's ranked  
programs are filled with preferred applicants and therefore applicant  
is not matched.
There is no need to test programs for completion, that is completion  
is an applicant's condition.

gen matched = .
gen pfilled = .
gen amatched = .

* Test if program has filled, mark pfilled = 1
forvalues p = 1/250{
      sum matched if pnum == `p'
      global temp2 = r(N)
      replace pfilled = 1 if $temp1 >= 50 & pnum == `p'
      drop $temp1
}

* unmatch applicant if below pfilled mark, number applicant position
     forvalues p = 1/250{
         gen appmp = _n if (matched != . | matched != 0) & pnum == `p'

         replace matched = 0 if appmp > 50 & pnum == `p'
         replace amatched = 0 if appmp > 50 & pnum == `p'
         replace amatched = 1 if appmp <= 50 & pnum == `p'
         *???????
         global am = anum if appmp <= 50 & pnum == `p' & amatched == 1
         replace amatched = 1 if anum == $am

* test if done ??????
     forvalues a = 1/12500{
         global temp1 = 0 if amatched == 0 & anum == `a'

Any help or ideas is greatly appreciated.

Thanks
Vincent








On Mar 17, 2008, at 8:12 AM, Joseph Coveney wrote:

> Vincent Davis asked about Stata's -while-:
>
>> When does it evaluate the WHILE condition?
>
> -while- evaluates the condition at the top (beginning) of the loop.   
> If you
> want to have the loop executed at least once regardless of the  
> condition,
> then you want a do-while or do-until loop: set up an infinite loop  
> as Nick
> Cox pointed out, and evaluate the condition of interest with an -if  
> (!<while
> condition>) continue, break- at the bottom (end) of the loop.  See  
> Example 1
> below.
>
>> If it becomes false does it immediately exit the WHILE loop?
>
> No; program flow will not immediately exit the loop midway through  
> if the
> condition becomes false during execution of the loop--the loop will  
> proceed
> to completion before the condition is re-evaluated at the top of the  
> next
> pass.  (I interpret Vincent's question differently from how Eva and  
> Nick
> did.)  See Example 2 below.  If you want to exit the loop as soon as  
> the
> condition becomes false, then test for the false condition (with -if
> (!<while condition>) continue, break-) inside the loop, immediately  
> after
> the condition is to be affected.  See Example 3 below.
>
> I agree with Kit that -forvalues- and -foreach- are modern  
> improvements over
> the old -for-, but while loops have some advantages over for loops in
> controlling program flow.  In addition -while- handles -continue,  
> break- in
> a manner consistent with most programmers' expectations.
>
> Joseph Coveney
>
> clear *
> set more off
> * Example 1
> local a 1
> local b 0
> while (1) {
>   display in smcl as result `a', `b' _newline(1)
>   display in smcl as text "do-until/while loop"
>   display in smcl as result `a', `++b'
>   if (`a' <= `b') continue, break
> }
> * Example 2
> local a 1
> local b 0
> while (`a' > `b') {
>   display in smcl as result `a', `b++' _newline(1)
>   display in smcl as text "If -while- exited " _continue
>   display "immediately, then you wouldn't have seen this:"
>   display in smcl as result `a', `b'
> }
> * Example 3
> local a 1
> local b 0
> while (`a' > `b') {
>   display in smcl as result `a', `b++' _newline(1)
>   if (`a' <= `b') continue, break
>   display in smcl as text "Immediate, midloop" _continue
>   display " evaluation, and you won't see this."
> }
> exit
>
>
> *
> *   For searches and help try:
> *   http://www.stata.com/support/faqs/res/findit.html
> *   http://www.stata.com/support/statalist/faq
> *   http://www.ats.ucla.edu/stat/stata/


*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index