Data visualization with SAS programming

Addind a dummy variable

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 91
Accepted Solution

Addind a dummy variable

Hi,

I have a data set which contains daily data. I need to fit an ARCH model to this data set.

How can I add a dummy variable to the heteroskedastic error part of garch model (sigma squared) so that the dummy variable equals to 1 for every Monday?

Thanks


Accepted Solutions
Solution
‎11-09-2012 04:22 PM
Esteemed Advisor
Posts: 7,293

Re: Addind a dummy variable

I'm not sure what you are trying to accomplish.  However, you are getting the missing row because you aren't account for the first row containing the variable names.

That said, your code tries to read dates, among other things, then replaces that data with another date.

Is the following all you are trying to do?:

PROC IMPORT OUT= WORK.TEST

            DATAFILE= "C:\ftse1.csv"

            DBMS=CSV REPLACE;

     GETNAMES=YES;

     DATAROW=2;

RUN;

data test;

  format date date9.;

  set test (rename=(date=_date));

  date=input(substr(_date,find(_date,"y,")+3),anydtdte25.);

  if weekday(date)=2 then flag=1;

  else  flag=0;

run;

View solution in original post


All Replies
Frequent Contributor
Posts: 129

Re: Addind a dummy variable

How about using day of week name (downame) format.  Something like this:

your_dummy_variable = (put(your_date_variable,downame.) = 'Monday' ) ;

Note this assumes your date variable is actually a sas date value and not a sas datetime value.

Valued Guide
Posts: 632

Re: Addind a dummy variable

A slight refinement from Larry's suggestion would be to use the weekday and IFN functions:

data want;

do date = '01nov12'd to '20nov12'd;

   flag = IFN(weekday(date)=2,1,0,.);

   put date date9. date flag;

end;

run;

Frequent Contributor
Posts: 91

Re: Addind a dummy variable

Thanks. But it gives me the following errorSmiley SadI'm using SAS 9.0)

data test;

45

46   do date = '01nov12'd to '20nov12'd;

47

48      flag = IFN(weekday(date)=2,1,0,.);

               ---

               68

ERROR 68-185: The function IFN is unknown, or cannot be accessed.

Malaka

Super User
Super User
Posts: 6,326

Re: Addind a dummy variable

So just use a normal IF/THEN/ELSE statement.  I am really not sure why anyone uses the IFN function anyway as it just adds confusion.

if weekday(date)=2 then flag=1;

else flag=0;

Or since SAS will generate boolean 0/1 values when evaluating logic equations why not just use:

flag = (weekday(date)= 2);

Esteemed Advisor
Posts: 7,293

Re: Addind a dummy variable

: I've gotten into the habit of using the ifn function because it removes so much confusion when dealing with lags (e.g.: http://www.sascommunity.org/wiki/Conditional_Use_of_LAG )

Frequent Contributor
Posts: 91

Re: Addind a dummy variable

Can you please see why the following code doesnt generate what I want

data test;

    infile "F:\data\ftse.csv"  DSD MISSOVER;

      input date open high low close returns d;

run;

data test;

set test;

do date = '01nov12'd to '20nov12'd;

   if weekday(date)=2 then flag=1;

   put date date9. date flag;

end;

run;

proc print data = test;

run;

Thanks

Respected Advisor
Posts: 4,973

Re: Addind a dummy variable

You are missing:

else flag=0;

Inside a DO loop, there is nothing in your code that resets FLAG.  Once it gets set to 1, it remains 1 for the rest of the loop.

Frequent Contributor
Posts: 91

Re: Addind a dummy variable

I'm still not getting the results I need.

I used the following code and Used a SMALL DATA SET(attached)

I need a new column added to the data set in which entry = 1 if the day is MONDAY.

Can you please CORRECT the following Code. (NEW data set attached)

Thanks

P.S. Why does the first row of the created data set contain "." s..?

data test;

    infile "F:\data\ftse1.csv"  DSD MISSOVER;

      input date open high low close returns;

run;

data test;

set test;

do date = '02jan90'd to '26jan90'd;

   if weekday(date)=1 then flag=1; else  flag=0;

   put date date9. date flag;

end;

run;

proc print data = test;

run;.

Attachment
Solution
‎11-09-2012 04:22 PM
Esteemed Advisor
Posts: 7,293

Re: Addind a dummy variable

I'm not sure what you are trying to accomplish.  However, you are getting the missing row because you aren't account for the first row containing the variable names.

That said, your code tries to read dates, among other things, then replaces that data with another date.

Is the following all you are trying to do?:

PROC IMPORT OUT= WORK.TEST

            DATAFILE= "C:\ftse1.csv"

            DBMS=CSV REPLACE;

     GETNAMES=YES;

     DATAROW=2;

RUN;

data test;

  format date date9.;

  set test (rename=(date=_date));

  date=input(substr(_date,find(_date,"y,")+3),anydtdte25.);

  if weekday(date)=2 then flag=1;

  else  flag=0;

run;

Frequent Contributor
Posts: 91

Re: Addind a dummy variable

all I want to do is to add a new column to that attached data set so that the new column contains 1 if the corresponding day is a Monday and zero other wise.

Could you please send me a code that does this.

I am still trying to understand this so its kind of hard to do it my self.

Thanks

Frequent Contributor
Posts: 129

Re: Addind a dummy variable

Art's code does exactly what you are asking for.  It adds the column called flag which has the requested values.

But at the risk of sounding pedogic, you need to understand what you are doing.

The purpose of forums like this is to share knowledge and help neophytes understand how to use SAS efficiently, not to get someone to write your homework or business assignment.

IMHO, it would help you to understand the changes which Art suggested.

Frequent Contributor
Posts: 91

Re: Addind a dummy variable

I understand what exactly you are saying. I am trying to learn it. I am still new to SAS.

Any way I appreciate your input.

Thank you very much.

Esteemed Advisor
Posts: 7,293

Re: Addind a dummy variable

The only thing you may have to change in the code I sent is the statement that identifies the file you are importing.

Rather than:


PROC IMPORT OUT= WORK.TEST

            DATAFILE= "C:\ftse1.csv"

            DBMS=CSV REPLACE;

     GETNAMES=YES;

     DATAROW=2;

RUN;

it should read:

PROC IMPORT OUT= WORK.TEST

            DATAFILE= "F:\data\ftse1.csv"

            DBMS=CSV REPLACE;

     GETNAMES=YES;

     DATAROW=2;

RUN;

The proc import reads your csv file and, in the process, gets the variable names.  Unfortunately, the date field is imported as text and I can't think of an informat that can correctly understand a date like "Frday, November 9, 2012" which is how the dates are represented in the file.

As such, I renamed the date field to be _date and then created a date field with the statement:

date=input(substr(_date,find(_date,"y,")+3),anydtdte25.);

The substr part gets everything to the right of the day names, and then the input function inputs the field using the anydtdte25. informat.

Super User
Super User
Posts: 6,326

Re: Addind a dummy variable

If you read in the CSV file and just keep the DATE field as a character string you can also create the new flag be just testing if the string starts with Monday.

So if you read it using Art's PROC IMPORT into a dataset named TEST then you can create a new dataset named WANT with the extra variable (column) named FLAG with this simple program.  The colon after the equal sign tells SAS to match strings up to the length of the shorter of the two.

data want;

  set test;

  flag = (date =: 'Monday') ;

run;

If your real data is messier than the sample so that it has mixed case text for the day of the week then you might want to include a LOWCASE() function call.

data want;

  set test;

  flag = (lowcase(date) =: 'monday') ;

run;

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 15 replies
  • 908 views
  • 12 likes
  • 7 in conversation