DATA Step, Macro, Functions and more

Identify new value of string variable

Reply
Regular Learner
Posts: 1

Identify new value of string variable

[ Edited ]

Hi there,

 

I am working with publication data. Starting with a set of cited publications, I am trying to characterize the set of citing publications that cite the cited publications. I am trying to define the following variables. 

 

New Key Word: defined as new if a key word has never before appeared in the key word list of the cited article or previous citations to the cited article; old otherwise.

 

Total New Key Words: total number of new keywords in citing article.

 

DATA cited;
INFILE CARDS;
INPUT CITED_ID KW_1 $ KW_2 $ KW_3 $;
CARDS;
1000 xx1 xx2 xx3
1001 aa1 aa2 aa3
;
run;

 

DATA citing;
INFILE CARDS;
INPUT CITED_ID CITING_ID KW_1 $ KW_2 $ KW_3 $;
CARDS;
1000 2000 xx1 xx2
1000 2001 yy1 xx2 yy2
1000 2002 xx3 yy1 yy2
1000 2003 yy2 xx2 zz1
1001 2004 aa1 aa2 xx1
1001 2005 aa1 bb1 bb2
1001 2006 aa1 bb2 cc1
;
run;

 

Desired Data 
CITED_ID CITING_ID NEW_KW TOT_NEW_KW
1000 2000 0 0
1000 2001 1 2
1000 2002 0 0
1000 2003 1 1
1001 2004 1 1
1001 2005 1 2
1001 2006 1 1

 

Thanks,

Gary

PROC Star
Posts: 1,561

Re: Identify new value of string variable

Like this?

 

data CITED;
  input CITED_ID KW_1 $ KW_2 $ KW_3 $;
cards;
1000 xx1 xx2 xx3
1001 aa1 aa2 aa3
run;

data CITING;
  infile cards missover;
  input CITED_ID CITING_ID KW_1 $ KW_2 $ KW_3 $;
cards;
1000 2000 xx1 xx2
1000 2001 yy1 xx2 yy2
1000 2002 xx3 yy1 yy2
1000 2003 yy2 xx2 zz1
1001 2004 aa1 aa2 xx1
1001 2005 aa1 bb1 bb2
1001 2006 aa1 bb2 cc1
run;

data WANT; 
  retain CITED_LIST ;
  length CITED_LIST $80;
  merge CITED 
        CITING(rename=(KW_1=K1 KW_2=K2 KW_3=K3));
  by CITED_ID;
  TOT_NEW_KW=0;
  if first.CITED_ID then do;
    CITED_LIST=catx(' ',of KW_1-KW_3);
    TOT_NEW_KW=0;
  end;    
  array K [3];
  do I=1 to 3;
    if K[I] ne ' ' & ^findw( CITED_LIST, K[I], , 'st') then do;       
      TOT_NEW_KW+1;
      CITED_LIST=catx(' ', CITED_LIST, K[I]);
    end;  
  end;  
  keep CITED_ID CITING_ID TOT_NEW_KW;
run;

SAS Output

CITED_ID CITING_ID TOT_NEW_KW
1000 2000 0
1000 2001 2
1000 2002 0
1000 2003 1
1001 2004 1
1001 2005 2
1001 2006 1

 

 

 

Super User
Posts: 9,671

Re: Identify new value of string variable

DATA cited;
INFILE CARDS;
INPUT CITED_ID KW_1 $ KW_2 $ KW_3 $;
CARDS;
1000 xx1 xx2 xx3
1001 aa1 aa2 aa3
;
run;
 
DATA citing;
INFILE CARDS truncover;
INPUT CITED_ID CITING_ID KW_1 $ KW_2 $ KW_3 $;
CARDS;
1000 2000 xx1 xx2
1000 2001 yy1 xx2 yy2
1000 2002 xx3 yy1 yy2
1000 2003 yy2 xx2 zz1
1001 2004 aa1 aa2 xx1
1001 2005 aa1 bb1 bb2
1001 2006 aa1 bb2 cc1
;
run;

proc transpose data=cited out=key(where=(col1 is not missing));
by cited_id;
var kw_:;
run;
data want;
 if _n_=1 then do;
  if 0 then set key;
  declare hash h(dataset:'key');
  h.definekey('cited_id','col1');
  h.definedone();
 end;
set citing;
array x{*} kw_: ;
tot_new_kw=0;
do i=1 to dim(x);
 if not missing(x{i}) then do;
  col1=x{i};
  if h.check() ne 0 then do;
   tot_new_kw+1;h.add();
  end;
 end;
end;
new_kw=(tot_new_kw ne 0);
drop _name_ i col1;
run;
Ask a Question
Discussion stats
  • 2 replies
  • 198 views
  • 0 likes
  • 3 in conversation