BookmarkSubscribeRSS Feed
sandhya88
Calcite | Level 5
Predefined criteria is flag the records for the id as Y if atleast one record with pstbsfl have result >=4*baseresult.
Data have;
Input Id visitn testcd result baseresult basefl pstbsfl

1 1 ALT 4 2 . Y
1 2 ALT 8 2 . Y
1 0 ALT 2 2 Y .
1 1 AST 2 2 . Y

;
Run;

Data want;
Id visitn testcd result baseresult basefl pstbsfl critfl

1 1 ALT 4 2 . Y Y
1 2 ALT 8 2 . Y Y
1 0 ALT 2 2 Y . Y
1 1 AST 2 2 Y . Y
;
Run;

The logic here is that as the id 1 is having atleast one record where result is 4* baseresult value ( visitn=2) I want to flag all the records for that id as critfl eq ‘Y’

Your response is appreciated.

Please help

Thanks

19 REPLIES 19
MayurJadhav
Quartz | Level 8

You can use count(*) on your have dataset with the condition and assign flag accordingly.

data have;
	Input Id visitn testcd $ result baseresult basefl $  pstbsfl $;
	datalines;

1 1 ALT 4 2 . Y
1 2 ALT 8 2 . Y
1 0 ALT 2 2 Y .
1 1 AST 2 2 . Y

;
run;

/* count how many records do you have with your if condition */
proc sql;
	select count(*) into:cnt_flg from want where result >=4*baseresult;
	%put cnt_flg= &cnt_flg;
quit;

/* flag Y/N on critfl */
data want;
	set have;

	if &cnt_flg >=1 then
		critfl='Y';
	else
		critfl='N';

proc print;
run;

MayurJadhav_0-1677022518071.png

 

Mayur Jadhav
BI Developer. Writer. Creative Educator.

SAS Blog → https://learnsascode.com
YouTube Channel: → https://www.youtube.com/@imayurj
s_lassen
Meteorite | Level 14

Make sure the data is sorted by ID, and then this:

data want;                                                                                                                              
  if 0 then set have; /* just to get same order of variables */                                                                         
  critfl='N';                                                                                                                           
  do until(last.id);                                                                                                                    
    set have;                                                                                                                           
    by id;                                                                                                                              
    if result>=4*baseresult then                                                                                                        
      critfl='Y';                                                                                                                       
    end;                                                                                                                                
  do until(last.id);                                                                                                                    
    set have;                                                                                                                           
    by id;                                                                                                                              
    output;                                                                                                                             
    end;                                                                                                                                
run;             
sandhya88
Calcite | Level 5

Thank you so much for the earlier response , just to add one more detail to my question my criterion is based on one test i.e ALT and if that test is not available for a ID Then the critfl should be left blank and not as "N".

 

Thanks in advance.

 

Your response is appreciated.

s_lassen
Meteorite | Level 14

I do not quite understand your question, could you give some example data?

sandhya88
Calcite | Level 5

Data have;

input id visitn test result baseresult basefl postbasefl;

1  1  ALT  12  3  . Y

1  2  AST  3    1  .Y

2  1  AST   3   1  Y .

;

run;

 

Data want;

input id visitn test result baseresult basefl postbasefl critfl;

1  1  ALT  12  3  . Y Y

1  2  AST  3    1  .Y  Y

2  1  AST   3   1  Y .  .

;

run;

For the ID 2 as we dont see any ALT at all that satisfies the predefined criteria result>=4*baseresult i want to see CRITFL as missing for the ID throughout.

 

PREDEFINED CRITERION: If any postbasefl result of an ID with ALT Is ge 4*baseresult then flag all the records for that ID or if postbasefl for the ID for ALT is missing then leave a null value in CRITFL.

 

Hopefully I am a bit clear now

 

THANKS ALOT

 

 

s_lassen
Meteorite | Level 14

Something like this?

data want;                                                                                                                              
  if 0 then set have; /* just to get same order of variables */                                                                         
  do until(last.id);                                                                                                                    
    set have;                                                                                                                           
    by id;                                                                                                                              
    if testcd='ALT' then do;                                                                                                            
      if result>=4*baseresult then                                                                                                      
        critfl='Y';                                                                                                                     
      else if critfl=' ' then                                                                                                           
        critfl='N';                                                                                                                     
      end;                                                                                                                              
    end;                                                                                                                                
  do until(last.id);                                                                                                                    
    set have;                                                                                                                           
    by id;                                                                                                                              
    output;                                                                                                                             
    end;                                                                                                                                
run;        
sandhya88
Calcite | Level 5
SO THE CRITFL SHOULD BE "Y" if there is any postbasefl record for the ID with ALT Result>=4*baseresult.
2.CRITFL should be "N" if there is no postbasefl record for the ID with ALT result NOT ge baseresult.
3.CRITFl should be "" (missing) if the ID Doesnt have any postbasefl record for the ALT test even if it has AST test.

The above response made the values NULL When the CRITFL should be "N".

Thanks once again for your prompt response.

Ksharp
Super User
data have;
 Input Id visitn testcd $ result baseresult basefl $  pstbsfl $;
 datalines;
1 1 ALT 4 2 . Y
1 2 ALT 8 2 . Y
1 0 ALT 2 2 Y .
1 1 AST 2 2 . Y
;
run;

proc sql;
create table want as
select *,ifc(max(result>=4*baseresult),'Y','N') as critfl
 from have
  group by id;
quit;
sandhya88
Calcite | Level 5

@Ksharp thanks for your response the response is not giving NULL values for the ID If the ID Doesnt have any postbaseresult record for ALT.

 

Thanks alot

 

 

sandhya88
Calcite | Level 5
these are my conditions to flag the CRITFL.
SO THE CRITFL SHOULD BE "Y" if there is any postbasefl record for the ID with ALT Result>=4*baseresult.
2.CRITFL should be "N" if there is no postbasefl record for the ID with ALT result NOT ge baseresult.
3.CRITFl should be "" (missing) if the ID Doesnt have any postbasefl record for the ALT test even if it has AST test.

Ksharp
Super User

OK.Try this one.(I did not test it)

P.S. You'd better post more output data to explain your question.

 

 

data have;
 Input Id visitn testcd $ result baseresult basefl $  pstbsfl $;
 datalines;
1 1 ALT 4 2 . Y
1 2 ALT 8 2 . Y
1 0 ALT 2 2 Y .
1 1 AST 2 2 . Y
;
run;

proc sql;
create table want as
select *,
case when max(pstbsfl='Y' and testcd='ALT' and result>=4*baseresult) then 'Y'
     when max(pstbsfl='Y' and testcd='ALT' and result<baseresult)=0 then 'N'
     when max(pstbsfl='Y' and testcd='ALT')=0 then ' '
else 'X' end as critfl
 from have
  group by id;
quit;
sandhya88
Calcite | Level 5

@Ksharp 

 

Thanks for your response again.

 

Data want;

input id visitn test result baseresult basefl postbasefl critfl;

1  1  ALT  12  3  . Y Y

1  2  AST  3    1  .Y  Y

2  1  AST   3   1  Y .  .

2  2  AST   2   1 . Y .

;

run;

For the ID 2 as we dont see any ALT at all that satisfies the predefined criteria result>=4*baseresult i want to see CRITFL as missing for the ID throughout.

 

PREDEFINED CRITERION: If any postbasefl result of an ID with ALT Is ge 4*baseresult then flag all the records for that ID or if postbasefl for the ID for ALT is missing then leave a null value in CRITFL.

 

the above response has populated "X' for CRITFL when none of the pstblfl records has result>=4*baseresult but whereas we need to see "N",please have a look the want dataset above .

 

Thanks

 

Ksharp
Super User
/*
It is hard to understand your logic.
Can you post tree example/output corresponding to your three conditions?
*/
data have;
 Input Id visitn testcd $ result baseresult basefl $  pstbsfl $;
 datalines;
1  1  ALT  12  3  .  Y 
1  2  AST  3    1  . Y  
2  1  AST   3   1  Y  .  
2  2  AST   2   1 .  Y 
;
run;

proc sql;
create table want as
select *,
case when max(pstbsfl='Y' and testcd='ALT' and result>=4*baseresult) then 'Y'
     when max(pstbsfl='Y' and testcd='ALT' and result>=4*baseresult)=0 then ' '
  when max(pstbsfl='Y' and testcd='ALT' and result<baseresult)=0 then 'N'
else 'X' end as critfl
 from have
  group by id;
quit;
sandhya88
Calcite | Level 5

@Ksharp thanks for your response.

 

here is what i want:

 

Data want;

input $id visitn $test result baseresult $basefl $postbasefl $critfl;

1  1  ALT  12  3  . Y  Y

1  2  AST  3    1  .Y  Y

2  1  AST   3   1  Y .  .

2  2  AST   2   1 . Y .

;

run;

LOGIC:

1.FOR CRITfl eq "Y"-THE RECORDS FOR ID HAVE BOTH ALT AND AST TESTS AND THE CRITFL SHOULD BE FLAGGED "Y" FOR ALL THOSE RECORDS OF THE ID IF THE ID HAS ATLEAST ONE RECORD WITH POSTBASEFL AS "Y" AND RESULT>=4*BASERESULT.

2. FOR CRITFL EQ "N" -FOR ID 1 IT SHOULD BE FLAGGED "N" if there is no record at all where postbasefl eq "Y"  and result>=4*baseresult..

3.FOR CRITFL eq ""- IF THE ID DOESNT HAVE ANY POSTBASEFL EQ "Y" RECORD FOR THE TEST ALT THEN THE ID SHOULD HAVE CRITFL "".

HERE IS THE FLOWCHART IN CASE IF THE LOGIC IS STILL NOT CLEAR

 

sandhya88_0-1677244081125.png

Your Response is much appreciated.

 

TIA

 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 19 replies
  • 1542 views
  • 1 like
  • 4 in conversation