Page 1 of 1

How to convert a categorical variable into dummies

Posted: Wed May 15, 2013 4:40 pm
by neophytosk
Dear all,

I have the following question.

Background

I have a timeseries dataset regarding fx trades with different counterparties as below. Key variables are the categorical variable 'counterparty type' indicating which is the originating side of a trade (who is trading), and some numerical variables, for illustration below I included flow(is the counterparty buying or selling).

My current dataset set looks as below:

time Currency counterparty type buy/sell
9am eur/usd bank 1
10am eur/usd corporate -1
11am eur/usd non bank financial -1
12pm eur/usd government 1
1pm eur/usd corporate 1


Question

Could you tell me how I can convert the categorical variable automatically to several dummies, each counterparty type should be a dummy with a 1 or 0 indicating whether there is a trade or not.(also all other variables need to be adjusted accordingly to the new structure e.g variable flow)

For illustration the code should convert the table to the below structure:

time Currency bank corporate non bank financial government buy/sell
9am eur/usd 1 0 0 0 1
10am eur/usd 0 1 0 0 -1
11am eur/usd 0 0 1 0 -1
12pm eur/usd 0 0 0 1 1
1pm eur/usd 0 1 0 0 1


I am aware of the @expand function however it only works within a regression framework and beforing running a regression I would liketo make some more changes to the basic dataset, e-g frequency compunding etc

Hope you are able to help - thanks!

Best

Neo

Re: How to convert a categorical variable into dummies

Posted: Wed May 15, 2013 5:15 pm
by startz

I have a timeseries dataset regarding fx trades with different counterparties as below. Key variables are the categorical variable 'counterparty type' indicating which is the originating side of a trade (who is trading), and some numerical variables, for illustration below I included flow(is the counterparty buying or selling).

My current dataset set looks as below:

time Currency counterparty type buy/sell
9am eur/usd bank 1
10am eur/usd corporate -1
11am eur/usd non bank financial -1
12pm eur/usd government 1
1pm eur/usd corporate 1


Question

Could you tell me how I can convert the categorical variable automatically to several dummies, each counterparty type should be a dummy with a 1 or 0 indicating whether there is a trade or not.(also all other variables need to be adjusted accordingly to the new structure e.g variable flow)
series bank = @recode(counterparty="bank",1,0)

Re: How to convert a categorical variable into dummies

Posted: Wed May 15, 2013 11:20 pm
by neophytosk
Hi Statz,

Thnx for the reply.

However to convert the counterparty type with the recode function I need to re write the command for all counterparty types in the variable.My example was very simplified. I am looking for a code to do this since I have many categorcial variables to convert into dummies and each one of them has 7-8 different types. Is there a more efficient way to do this?

Neo

Re: How to convert a categorical variable into dummies

Posted: Thu May 16, 2013 6:19 am
by startz
You can create a group with

Code: Select all

group g @expand(counterparty)
and then refer to the series within that group as g(1), g(2), etc.