04-02-2012 10:27 PM
I have a question in regard to finding the common elements in two vectors.
I have a column vector of tickers for a stock index. I have a loop over time which does some calculations at each day.
At each day inside the time loop, I have around 40 tickers, which are repeating again and again through time.
However, in some days new stocks may introduce to the index so the column vector of tickers at day t may have one of two tickers more than the column vector of tickers at day t-1.
For example, as it can be seen in the following table, at day t, NOP enters into the index for the first time. To observe these new stocks, I want to introduce a new variable as NEW which gives me zero for each common ticker and one for the new tickers which enter the index for the first time. So, how to define variable NEW in SAS/IML environment?
Your advice is appreciated.
|DAY (T-1)||DAY (T)||NEW|
04-03-2012 03:24 AM
input old $4. new $4.;
AXA AXA 0
AIA AIA 0
BCD BCD 0
FEJ FEJ 0
JUD JUD 0
KID KID 0
LOD LOD 0
MIN MIN 0
OOP NOP 1
QRE OOP 0
TRE QRE 0
WTF TRE 0
XDE WTF 0
ZSW XDE 0
/* ~~~~~~~~~~~~~~~~~~~~~ IML Code ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
Valid_Rows = nrow( old[loc(old ^= '')] );
Valid_Old = old[loc(old ^= '')];
Mesh = repeat(new,1,valid_rows);
old_reshape = repeat(valid_old,1,nrow(mesh))`;
New_ID = (old_reshape = mesh)[,+] = 0;
print new New_ID;
04-03-2012 06:37 AM
I suggest using the ELEMENT function in SAS/IML 9.3.
To use Ajay's data:
/* b = elements of NEW that are elements of OLD */
b = element(new, old);
New_ID = ^b; /* change 0 <--> 1 */
print new New_ID;
If you don't have SAS 9.3, I can suggest another way.
04-03-2012 06:57 AM
Yes please, I am using SAS 9.2.
For further clarification, I have column vectors DAY(T) and DAY(T-1) and want to produce the column NEW by having a loop over DAYs columns.
04-03-2012 08:10 AM
Here's a SAS/IML module that duplicates the functionality of the ELEMENT function that was introduced in SAS/IML 9.3:
start elementMod(x, y);
if ncol(x)=0 then return(_NULL_);
b = j(nrow(x), ncol(x), 0);
if ncol(y)=0 then return(b);
u = xsect(x, y);
if ncol(u)=0 then return(b);
do i = 1 to ncol(u);
b = bor(b, (x=u));
New_ID2 = ^elementMod(new, old);