BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Gregorytus07
Fluorite | Level 6

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User
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.


 

View solution in original post

5 REPLIES 5
PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
Gregorytus07
Fluorite | Level 6

@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

Reeza
Super User
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.


 

Gregorytus07
Fluorite | Level 6

@Reeza Thanks for sharing this. I will try it out.

Astounding
PROC Star

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;

sas-innovate-2024.png

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.

 

Register now!

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
  • 5 replies
  • 415 views
  • 3 likes
  • 4 in conversation