DATA Step, Macro, Functions and more

how to flag based on a variable value

Reply
Super Contributor
Posts: 272

how to flag based on a variable value

Dear, 

 

I need help in my code. One of support team help me with following code. But I need one more condition to include before assigning a flag value. I need TO ASSIGN  flag values only for obs with variable type='R'. Please help in the code.

 

I tried by sorting the data have by "type"  "id". Then I used "if type='R' then do"  in the code. The output also flagged obs where type='M'. 

 

Thank you

 

ouput needed;

 

id  type  test visit    visitwindow          date                 flag
1      R    dbp   1      1                 2014-07-30                Y
1      R    dbp   un     1               2014-08-10
2      R    dbp    1     1                2014-07-30
2     R      dbp    1     1                2014-08-10                 Y
3      R    dbp    un    1                2014-07-30
3       R     dbp    un    1                2014-08-10                 Y

4      R      dbp      1    1                2014-07-30                 Y

1      M     dbp      1     1                2014-07-30
1      M     dbp     un    1                2014-08-10
2     M     dbp       1     1                2014-07-30
2    M      dbp      1      1                 2014-08-10
3     M     dbp     un     1                 2014-07-30
3     M     dbp     un     1                 2014-08-10
4    M      dbp      1      1                 2014-07-30

 

 

data have;
input id type $ test$ visit $  visitwindow vsdtc :yymmdd10.;
format vsdtc date9. ;
datalines;
1 R dbp 1 1 2014-07-30
1 R dbp un 1 2014-08-10
2 R dbp 1 1 2014-07-30
2 R dbp 1 1 2014-08-10
3 R dbp un 1 2014-07-30
3 R dbp un 1 2014-08-10
4 R dbp 1 1  2014-07-30    
1 M dbp 1 1 2014-07-30
1 M dbp un 1 2014-08-10
2 M dbp 1 1 2014-07-30
2 M dbp 1 1 2014-08-10
3 M dbp un 1 2014-07-30
3 M dbp un 1 2014-08-10
4 M dbp 1 1  2014-07-30 
run;

code:

data want;
 do i=1 by 1 until(last.id);
  set have;
  by id type;
  if visit=1 then idx=i;
 end;

 do j=1 by 1 until(last.id);
  set have;
  by id type;
  flag=' ';
  if idx=j then flag='Y'; 
  if last.id and missing(idx) then flag='Y';
  output;
 end;
end;
drop i j idx;
run;



 

Valued Guide
Posts: 634

Re: how to flag based on a variable value

[ Edited ]
Posted in reply to knveraraju91

Nominally you can check for a given type using conditional processing.  Currently you have:


  if last.id and missing(idx) then flag='Y';

You can add a comparison for TYPE:


  if last.id and missing(idx) and type='R' then flag='Y'; 

 However your code has some deeper issues. 

A SORT is going to be needed before you can use the BY statement as it is currently written.  Have you tried the second DATA step?  Is it working for you?

Visit is treated at numeric, but is character in HAVE.  There is an extra END statement.  Get the code working and see if it still does not do what you need it to do.

PROC Star
Posts: 7,474

Re: how to flag based on a variable value

Posted in reply to knveraraju91

Your code won't even run without some minor changes. However, given its current structure, it already provides what you want if provided with data that is properly sorted. e.g.,

 

proc sort data=have out=want;
 by id type;
run;

data want;
 do i=1 by 1 until(last.id);
  set want;
  by id type;
  if visit='1' then idx=i;
 end;

 do j=1 by 1 until(last.id);
  set want;
  by id type;
  flag=' ';
  if idx=j then flag='Y'; 
  if last.id and missing(idx) then flag='Y';
  output;
 end;
drop i j idx;
run;

Art, CEO, AnalystFinder.com

Ask a Question
Discussion stats
  • 2 replies
  • 127 views
  • 0 likes
  • 3 in conversation