BookmarkSubscribeRSS Feed
sandhya88
Calcite | Level 5
Hi here is the example of the data I have

Data have;
Input $ID $test visitnum $basefl $result adt randdt

1 CHOL 1 Y HIGH 17JUN2018 18JUN2018
1 CHOL 1 Y LOW 17JUN2018 18JUN2018
1 CHOL 1 Y HIGH 18JUN2018 18JUN2018
1 CHOL 2 “” LOW 24JUN2018
18JUN2018;
run;

I want the basec to be populated as follows:

Data have;
Input $ID $test visitnum $basefl $result adt randdt $basec

1 CHOL 1 Y HIGH 17JUN2018 18JUN2018 HIGH
1 CHOL 1 Y LOW 17JUN2018 18JUN2018 HIGH
1 CHOL 1 Y HIGH 18JUN2018 18JUN2018 HIGH
1 CHOL 2 “” LOW 24JUN2018
18JUN2018 HIGH;
run;

Logic that I want here is that:
1. If a ID is having 2 or more records with BASEFL eq ‘Y’ and are equally apart from randdt then consider worst case (HIGH) as BASEC and retain the value across the ID.
2. If a ID is having 2 or more records with BASEFL eq ‘Y’ and are not equally apart from RANDDT then choose the BASEC from the closest record to RANDDT and retain its value across the ID.

Please help

Thanks




2 REPLIES 2
ballardw
Super User

Define what "equally apart" in terms of the variables shown means. "apart" from what needs to be part of the definition.

 

What is that “” supposed to indicate?

 

Please try to make sure that a data step will run and produce data similar to your actual use. I think that you meant something more like:

 Data have;
Input ID $ test $ visitnum basefl $ result $ adt :date9. randdt :date9.;
format adt randdt date9.;
datalines;
1 CHOL 1 Y HIGH 17JUN2018 18JUN2018
1 CHOL 1 Y LOW 17JUN2018 18JUN2018
1 CHOL 1 Y HIGH 18JUN2018 18JUN2018
1 CHOL 2 . LOW 24JUN2018 18JUN2018
;
run;

Note that the $ goes after the variable name to indicate that the variable is character. Your date variables need to be read with a date informat to have any change of determining any reasonable definition of "equally apart". Note that with inline data, you need to tell SAS where the data starts with a Datalines or Cards statement. The end of data semicolon must be on its own line, otherwise data on the line with the semicolon is ignored. To indicate a missing value, even for character data, you can use a period. The quotes would yield something quite different.

Also, it is best to paste code into a text box opened on the forum with the </> icon above the message window as the main message windows will reformat pasted text and can result in data step code that may not run. Plus the box helps delineate the code from discussion.

 

If you data is supposed to be significantly different than the above data step creates then show use in some more detail.

 


@sandhya88 wrote:
Hi here is the example of the data I have

Data have;
Input $ID $test visitnum $basefl $result adt randdt

1 CHOL 1 Y HIGH 17JUN2018 18JUN2018
1 CHOL 1 Y LOW 17JUN2018 18JUN2018
1 CHOL 1 Y HIGH 18JUN2018 18JUN2018
1 CHOL 2 “” LOW 24JUN2018
18JUN2018;
run;

I want the basec to be populated as follows:

Data have;
Input $ID $test visitnum $basefl $result adt randdt $basec

1 CHOL 1 Y HIGH 17JUN2018 18JUN2018 HIGH
1 CHOL 1 Y LOW 17JUN2018 18JUN2018 HIGH
1 CHOL 1 Y HIGH 18JUN2018 18JUN2018 HIGH
1 CHOL 2 “” LOW 24JUN2018
18JUN2018 HIGH;
run;

Logic that I want here is that:
1. If a ID is having 2 or more records with BASEFL eq ‘Y’ and are equally apart from randdt then consider worst case (HIGH) as BASEC and retain the value across the ID.
2. If a ID is having 2 or more records with BASEFL eq ‘Y’ and are not equally apart from RANDDT then choose the BASEC from the closest record to RANDDT and retain its value across the ID.

Please help

Thanks





 

sandhya88
Calcite | Level 5
Data want;
Input ID $ test$ visitnum basefl$ result$ adt randdt basec$

1 CHOL 1 Y HIGH 17JUN2018 18JUN2018 HIGH
1 CHOL 1 Y LOW 17JUN2018 18JUN2018 HIGH
1 CHOL 1 Y HIGH 18JUN2018 18JUN2018 HIGH
1 CHOL 2 . LOW 24JUN2018 18JUN2018 HIGH;
run;

@ballardw 

Thanks for your response and apologies for so many confusions.

 

Here below is what i meant regarding equally apart and the data is how you have shown in your answer and here above is the data look that i want finally.

 

 

when I meant equally apart i mean to say that when we calculate the difference between adt and randdt if we get the same value for any two records.

 


Logic that I want here is that:
1. If a ID is having 2 or more records with BASEFL eq ‘Y’ and are equally apart (difference between adt and randdt is same )from randdt then consider worst case (HIGH) as BASEC and retain the value across the ID.
2. If a ID is having 2 or more records with BASEFL eq ‘Y’ and are not equally apart from RANDDT then choose the BASEC from the closest record to RANDDT and retain its value across the ID.

 

TIA again

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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