Hi everyone,
I have a dataset that contains multiple result rows for each study ID. I need help in writing a SAS code that selects the first result for each study ID. If the first result is 'See Text', then select the next result value for that study ID.
Thanks for your help.
data want;
set have;
by studyID;
where firstResult ne 'See Text';
if first.studyID;
run;
Use FIRST assuming it's sorted and a WHERE to remove the SEE TEXT records.
@Gregorytus07 wrote:
Hi everyone,
I have a dataset that contains multiple result rows for each study ID. I need help in writing a SAS code that selects the first result for each study ID. If the first result is 'See Text', then select the next result value for that study ID.
Thanks for your help.
Please clarify: your title says "Variable Selection" but your description implies you want to select rows. Which is it?
In either case, please provide sample data, and show us the desired output.
@PaigeMiller Thanks for your feedback.
This is a sample data
DATA YOUR_DATASET;
INPUT STUDYID GLUCOSE$;
DATALINES;
1 100
1 120
1 See Text
2 85
2 95
3 See Text
3 110
4 See Text
4 80
4 90
;
RUN;
The desired output should be similar to this;
Study_id | Glucose |
1 | 100 |
2 | 85 |
3 | 110 |
4 | 80 |
data want;
set have;
by studyID;
where firstResult ne 'See Text';
if first.studyID;
run;
Use FIRST assuming it's sorted and a WHERE to remove the SEE TEXT records.
@Gregorytus07 wrote:
Hi everyone,
I have a dataset that contains multiple result rows for each study ID. I need help in writing a SAS code that selects the first result for each study ID. If the first result is 'See Text', then select the next result value for that study ID.
Thanks for your help.
@Reeza Thanks for sharing this. I will try it out.
Is it possible that a StudyID has all its observations saying "No Text"? If so, the proposed solution will exclude that StudyID entirely. To guarantee selecting an observation for each StudyID, a slight variation would do the trick:
data want;
set have (where=(firstResult ne 'See Text'))
have (where=(firstResult eq 'See Text'));
by studyID;
if first.studyID;
run;
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.