How can I generate dummy variables manually?

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 109
Accepted Solution

How can I generate dummy variables manually?

[ Edited ]

Hello everybody;

 

Here is my code:

 

DATA Sampledata87_02_Mer_DumVar;
  set Sampledata87_02_Mer;
 
  ARRAY dummys {*} 8.  TRD_EVENT_ROUFOR_1 - TRD_EVENT_ROUFOR_9;
 
  DO i=1 TO 9;			      
    dummys(i) = 0;
  END;
  dummys( TRD_EVENT_ROUFOR  ) = 1;		
 
RUN;
 
PROC FREQ DATA=Sampledata87_02_Mer_DumVar;
  TABLES TRD_EVENT_ROUFOR*TRD_EVENT_ROUFOR_1*TRD_EVENT_ROUFOR_2*TRD_EVENT_ROUFOR_3*TRD_EVENT_ROUFOR_4*TRD_EVENT_ROUFOR_5*TRD_EVENT_ROUFOR_6*TRD_EVENT_ROUFOR_7*TRD_EVENT_ROUFOR_8*TRD_EVENT_ROUFOR_9 / list ;
RUN;


The log is:

220  DATA Sampledata87_02_Mer_DumVar;
221    set Sampledata87_02_Mer;
222
223    ARRAY dummys {*} 8.  TRD_EVENT_ROUFOR_1 - TRD_EVENT_ROUFOR_9;
224
225    DO i=1 TO 9;
226      dummys(i) = 0;
227    END;
228    dummys( TRD_EVENT_ROUFOR  ) = 1;
229
230  RUN;


ERROR: Array subscript out of range at line 228 column 3.
TRD_EVENT_DT=. TRD_EVENT_TM=  TRD_STCK_CD=  TRD_EVENT_ROUFOR=  CountedVOLUME=. IntradayVolume=.
Volume=8.2246061E13 adjusted_volume=. TRD_EVENT_ROUFOR_1=0 TRD_EVENT_ROUFOR_2=0
TRD_EVENT_ROUFOR_3=0 TRD_EVENT_ROUFOR_4=0 TRD_EVENT_ROUFOR_5=0 TRD_EVENT_ROUFOR_6=0
TRD_EVENT_ROUFOR_7=0 TRD_EVENT_ROUFOR_8=0 TRD_EVENT_ROUFOR_9=0 i=10 _ERROR_=1 _N_=1

WARNING: The data set WORK.SAMPLEDATA87_02_MER_DUMVAR may be incomplete.  When this step was
         stopped there were 0 observations and 18 variables.
WARNING: Data set WORK.SAMPLEDATA87_02_MER_DUMVAR was not replaced because this step was stopped.


What is the problem? Which part of my code is wrong?

Thanks!

 

 

 


Accepted Solutions
Solution
‎05-26-2017 07:13 AM
Super User
Posts: 6,946

Re: How can I generate dummy variables manually?

We already know what's happening. Make TRD_EVENT_ROUFOR numeric, and make sure that the values are in the range suitable for your array.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers

View solution in original post


All Replies
Super User
Super User
Posts: 7,407

Re: How can I generate dummy variables manually?

Look at your error mesaage:

ERROR: Array subscript out of range at line 228 column 3.
TRD_EVENT_DT=. TRD_EVENT_TM=  TRD_STCK_CD=  TRD_EVENT_ROUFOR=  CountedVOLUME=. IntradayVolume=.

This is clearly showing both that A) trd_event_roufor is missing - which is invalid for an array index, and B) it is a character variable - you want a numeric variable.

 

In future, it is a good idea to post test data in the form of a datastep so we don't have to investigate these things (also code in lower case, it makes it far easier to read).

 

 

 

 

Super User
Posts: 6,946

Re: How can I generate dummy variables manually?

That:

NOTE: Character values have been converted to numeric values at the places given by:

is the pointer to the sloppy programming.

See my Maxim 25 (Clean log). Such NOTEs must not be tolerated, as they indicate that the programmer did not really take care of the type of one of the variables. Also see Maxim 3 (Know your data).

 

So you need to take a good look at the contents of this variable throughout the dataset (following Maxim 3), and convert it to numeric in the first place. A missing value cannot be tolerated if you want to use it for indexing into an array.

This will then mean that you follow Maxim 25.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Frequent Contributor
Posts: 109

Re: How can I generate dummy variables manually?

The CONTENTS Procedure:
Alphabetic List of Variables and Attributes # Variable Type Len Format Informat Label 5 6 1 4 2 3 7 8
CountedVOLUMENum8   
IntradayVolumeNum8   
TRD_EVENT_DTChar10$10.$10.TRD_EVENT_DT
TRD_EVENT_ROUFORChar5   
TRD_EVENT_TMChar8$8.$8.TRD_EVENT_TM
TRD_STCK_CDChar5$5.$5.TRD_STCK_CD
VolumeNum8  Volume
adjusted_volumeNum8   
Solution
‎05-26-2017 07:13 AM
Super User
Posts: 6,946

Re: How can I generate dummy variables manually?

We already know what's happening. Make TRD_EVENT_ROUFOR numeric, and make sure that the values are in the range suitable for your array.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
☑ This topic is SOLVED.

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

Discussion stats
  • 4 replies
  • 153 views
  • 2 likes
  • 3 in conversation