How to easily define a constant variable in a panel in Stata - panel

I have a panel dataset (country-year) in Stata. For instance I have GDP in 1990,1991,..2010 for many countries.
I want to define a variable "GDP in 2006" which exists for all years and contains the 2006 value of GDP.
The way I am doing it now works but is a but clumsy so I was hoping someone would have a better idea:
qui gen gdp2006=.
replace gdp2006=gdp if year==2006
forval t=2007/2010 {
sort country year
qui replace gdp2006=gdp2006[_n-1] if year==`t'&country[_n-1]==country
}
forval t=2005(-1)1990 {
sort country year
qui replace gdp2006=gdp2006[_n+1] if year==`t'&country[_n+1]==country
}
Thanks!

You can do this in one line
egen gdp2006 = mean(gdp / (year == 2006)), by(country)
(year == 2006) evaluates as 1 or 0, so the expression
gdp / (year == 2006)
evaluates as gdp when year is 2006 and missing otherwise. Missings are ignored in calculating the mean for each country.
For a wider and more systematic discussion see http://www.stata-journal.com/article.html?article=dm0055 (which will be accessible to all in a few weeks from this posting).
P.S. The techniques you know permit shortening of your code:
gen gdp2006 = gdp if year == 2006
bysort country (gdp2006): replace gdp2006 = gdp2006[_n-1] if _n > 1

gen gdp2006_temp = gdp if year == 2006
bys country : egen gdp2006 = total(gdp2006_temp)
drop gdp2006_temp
The key here is to use the by: prefix and the fact that the total() egen function ignores missing values.

Related

Outputting Integers Greater than a given number in Prolog

I want to display all cars that were manucfactured after 1997.
How can it be done using nothing than queries?
These are the predicates
/* type(type reference, type of car) */
car_type(01,hatchback).
car_type(02,van).
car_type(03,off_road).
/* car(make ref, type ref, model, reg no, colour, year) */
car(01,01,escort,fsd127,blue,1999).
car(07,01,uno,tre333,blue,1996).
car(02,03,discovery,uje777,red,1995).
car(03,01,starlet,uij236,green,1991).
car(01,02,transit,ski432,green,1991).
car(07,01,bravo,juy677,red,1998).
Below is the query I inputted.
?- car(_,TYPE_REF,_,_,_,(X#>1997)),
car_type(TYPE_REF, TYPE_OF_CARS)
With the error being
false
I want to see the following output
1999 HATCHBACK
1998 HATCHBACK
Your problem is that Prolog does not evaluate arguments inside queries (the procedure you are querying may evaluate them though, but is not what happens in your fact-based source).
So you are passing the term (X#>1997) as the last argument to car/6 which will not unify with any fact.
You may instead use a free variable to query every car and then constrain the value it is bound to:
?- car(_,TYPE_REF,_,_,_,Year), Year > 1997, car_type(TYPE_REF, TYPE_OF_CARS).
TYPE_REF = 1,
Year = 1999,
TYPE_OF_CARS = hatchback ;
TYPE_REF = 1,
Year = 1998,
TYPE_OF_CARS = hatchback.
Or you may start using CLP(fd) and add the constraint first:
?- Year #> 1997, car(_,TYPE_REF,_,_,_,Year), car_type(TYPE_REF, TYPE_OF_CARS).
Year = 1999,
TYPE_REF = 1,
TYPE_OF_CARS = hatchback ;
Year = 1998,
TYPE_REF = 1,
TYPE_OF_CARS = hatchback.

VB6 week of day function

I'm relatively new to VB6 and I've just been given an assignment where I have a date - for example '4/12/2016' - from this date, i'm trying to find out the day that it is. So let's say it's a wednesday. Now from this day, I'm trying to determine the dates for the week [sun(startdate) - sat(enddate)). How would I go about doing something like this?
EDIT: I have a pretty good idea about finding out the date for sunday and saturday, since I can simply do something along the lines...
dim dateStart,dateend as date
Ex of date given to me = '4/12/2016'
Dim dateDay as variant
dateDay = whatever I get here - i'm assuming that a date will return a number for whatever day it is ???? Not sure
Select Case dateDay
case 1 -Monday?
dateStart=dateadd("d",-1,'4/12/2016)
dateEnd = dateadd("d",6, '4/12/2016)
case 2 -Tuesday?
datestart = dateadd("d",-2,'4/12/2016)
dateend = dateadd("d",5,'4/12/2016)
End Select
Basically do the SELECT statement for all cases. Am I on the right track?
This code:
Debug.Print Format(DatePart("w", Now), "dddd")
will print whatever day of the week it is now to the Immediate window. If you want the abbreviated day of week, use "ddd" for the format.
Now, this code:
Dim DOW As String
Select Case DatePart("w", Now)
Case vbSunday
DOW = "Sunday"
Case vbMonday
DOW = "Monday"
Case vbTuesday
DOW = "Tuesday"
Case vbWednesday
DOW = "Wednesday"
Case vbThursday
DOW = "Thursday"
Case vbFriday
DOW = "Friday"
Case vbSaturday
DOW = "Saturday"
End Select
Debug.Print DOW
will do the same thing. However, it shows you how to evaluate programmatically which day of the week you're dealing with, by using vbSunday, vbMonday, etc. That should give you what you need to get started on your Select statement. To use your example, DatePart("w", "4/12/2016") evaluates to 3, or vbTuesday.
VB6 reference documentation is here, and rather well hidden I might add. Look up Format and DatePart to get familiar with other options.
EDIT: As MarkL points out, the Weekday function is available in VB6 (I thought it wasn't), and is simpler (one less argument) than using DatePart. This code:
Debug.Print Format(Weekday(Now), "dddd")
will also print whatever day of the week it is to the immediate window. jac has also provided a link to the Weekday function in the comments above.
You can try below codes, The code will return name of the day.
txtDateTime.Text = WeekdayName(Weekday(Now))
txtDateTime.Text = WeekdayName(Weekday(12 / 30 / 1995))
txtDateTime.Text = WeekdayName(Weekday(Date))

How stock a numeric value (diff of 2 date)

I've to calculate the différence between two Dates : TODAY() and DATE_DEB_VAC.
With Oracle, it's kinda easy : TODAY()-DATE_DEB_VAC -> give the number of day between those 2 date.
But I've to do it with in an ETL (GENIO). I've a column to stock it like that :
NUMBER_DAY_DIFF (NUMBER 10) = TODAY()-DATE_DEB_VAC. But it's impossible to stock it cause it's 2 date.
How can i do this ? :(
You can try the val function of GENIO ETL
VAL(TODAY()-DATE_DEB_VAC)
this is equivalent to to_numbre in Oracle
NUMBER_DAY_DIFF (NUMBER 10) = DATEDIFF (TODAY; DATE_DEB_VAC)
Should give you what you need.

Calculating holidays

A number of holidays move around from year to year. For example, in Canada Victoria day (aka the May two-four weekend) is the Monday before May 25th, or Thanksgiving is the 2nd Monday of October (in Canada).
I've been using variations on this Linq query to get the date of a holiday for a given year:
var year = 2011;
var month = 10;
var dow = DayOfWeek.Monday;
var instance = 2;
var day = (from d in Enumerable.Range(1,DateTime.DaysInMonth(year,month))
let sample = new DateTime(year,month,d)
where sample.DayOfWeek == dow
select sample).Skip(instance-1).Take(1);
While this works, and is easy enough to understand, I can imagine there is a more elegant way of making this calculation versus this brute force approach.
Of course this doesn't touch on holidays such as Easter and the many other lunar based dates.
And it gets complicated if you have to consider non-christian holidays. For example, jewish holidays are based on the jewish calender..
Maybe not so elegant, but more error prone - you can just add a table of all relevant holidays for the next 100 years.
int margin = (int)dow - (int)new DateTime(year, month, 1).DayOfWeek;
if (margin < 0) margin = 7 + margin;
int dayOfMonth = margin + 7*(instance - 1) //this is for 0-based day number, add 1 if you need 1-based. instance is considered to be 1-based
Please note that I wrote it without trying to compile, so it is to give the idea, but may require some clean up. Hope this helps!

calulate according to the DOB given

I made student list containing roll no.,name, gender, DOB,Age ,marks1,marks2,marks3,total and avg.
i want to right a code for age
that is when i click on age it should display the correct age according to the DOB given.
can you help using textbox events in c#
The algorithm should be
ageInYears = currentYear - birthYear - (birthMonth <= currentMonth && birthDay <= currentDay ? 0 : 1);
Subtract the years, and subtract an additional year if they haven't had their birthday yet this year.

Resources