BookmarkSubscribeRSS Feed
knveraraju91
Barite | Level 11

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;



 

2 REPLIES 2
ArtC
Rhodochrosite | Level 12

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.

art297
Opal | Level 21

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

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
  • 2 replies
  • 992 views
  • 0 likes
  • 3 in conversation