BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
malakaext
Calcite | Level 5

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

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

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

15 REPLIES 15
LarryWorley
Fluorite | Level 6

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.

ArtC
Rhodochrosite | Level 12

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;

malakaext
Calcite | Level 5

Thanks. But it gives me the following error:(I'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

Tom
Super User Tom
Super User

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);

art297
Opal | Level 21

: 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 )

malakaext
Calcite | Level 5

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

Astounding
PROC Star

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.

malakaext
Calcite | Level 5

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;.

art297
Opal | Level 21

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;

malakaext
Calcite | Level 5

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

LarryWorley
Fluorite | Level 6

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.

malakaext
Calcite | Level 5

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.

art297
Opal | Level 21

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.

Tom
Super User Tom
Super User

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;

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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