BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
joebacon
Pyrite | Level 9

Hi all,

 

I am back with yet another question.

 

I am working with the same dataset which looks like this:NLYS.PNG

 

Currently, I am trying to break apart my dataset to grab occurrences of those whose BMI at age 18 is between 20-30. I can get close, but when i add the restrictions, it restricts the BMI after age 18 as well. I have tried a select statement as well as just making a new dataset. My thoughts are that I might have to utilize first obs? 

 

My ultimate goal is to create a spider/spaghetti plot to track these individuals then project their BMI after their last visit (which I am leaning towards utilizing Enterprise Miner).

 

However, right now if someone could give me guidance into what the best route to restrict the dataset to that condition only at age 18, but not the rest of the longitudinal data, that would be great.

 

Here are two of my most recent goes at it:

data obese2;
set BMI_DOB;
If age = 18 and 30>=BMI >= 20 then Class = 'Yes';
Else if age = 18 and 20<BMI >30 then Class = 'No';
run;

data obese2;
set BMI_DOB;
select (age)(BMI);
when (18) and (30) >=BMI >= (20) do;
Class = 'Yes';
end;
otherwise do;
Class = 'No';
end;
end;
run;

data obese;
	set BMI_DOB;
	where age ge 18 and 30 >=BMI >= 20;
run;

 

 

The first bit of code gives me my classes but when I try to plot it on SGPlot, i cant get the observations after age 18:

 

proc sgplot data = obese2;
	where ID LE 30 and class = 'Yes';
	series x = Age y = BMI / group= Gender break curvelabel;
	yaxis values=(20 to 36 by 2);
xaxis values= (18 to 35 by 1 );
run;

Whereas, the last bit from the top part gives me the graph I am looking for, but if they go above 30BMI or below 20, the line stops

:

proc sgplot data = obese;
	where ID LE 30;
	series x = Age y = BMI / group= ID break curvelabel;
	yaxis values= (20 to 36 by 2);
	xaxis values= (18 to 35 by 1 );
run;

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User
Make this two steps. First identify all individuals who are 18 and BMI is normal. Then filter your main data set for just those IDs using a MERGE or SQL.

proc sql;
create table want as
select * from have
where id in (Select id from have where age=18 and bmi between 20 and 30);
quit;

View solution in original post

5 REPLIES 5
PaigeMiller
Diamond | Level 26

In the future, showing us a picture of your data isn't helpful. It would be more helpful if you can provide us with actual SAS Code to create your data set, so that WE CAN try to run your code. https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat...

 

But we can't run your code, because we don't have your data.

 

What ERRORs are you getting? You description is not really helpful here. You're not getting what you want, what are you getting?

 

If there are errors in the SASLOG, show us the relevant portions of the SASLOG. Use the {i} button and then paste the relevant portions of your SASLOG into the window that appears.

 

Lastly, in your other thread, I suggested you create a variable that indicates which group the patient is in and then doing PROC SGPLOT using the BY statement. I still think that will work well here, better than anything I see you trying.

--
Paige Miller
joebacon
Pyrite | Level 9

Hi Paige!

 

Sorry for the picture. That didnt occur to me, actually but makes perfect sense! Excuse my naivete, but is there a way to copy and paste the data efficiently?

 

I am trying to create the variable like you said! If you look at the first bit of code, I was intending to create a "yes" and "no" category and I have been working off that premise using different variations of this:

 

data obese2;
set BMI_DOB;
If age = 18 and 30>=BMI >= 20 then Class = 'Yes';
Else if age = 18 and (20>BMI or BMI >30) then Class = 'No';
run;

data obese2;
set obese2;
if ID >0 and Class= 'Yes'  then  Class = 'Yes';
else if ID > 1 and Class='No'  then Class='No';
run;

Basically, I got the part where when age=18 and its in that BMI range i get a yes. Otherwise, I get a no. However, for the rest of the occurences of that ID, I can not get them all to populate "Yes" or "No".

 

If I try make a category, then if the same ID goes above 30 BMI then it changes the Class from "Yes" to "No" if that makes sense.

 

I will attempt to put my data here. I apologize. 

 

data WORK.BMI_DOB;
  infile datalines dsd truncover;
  input ID:32. Gender:32. DOB_M:32. DOB_Y:32. Race:32. Year:32. Height:32. Weight:32. BMI:6.3 DOB:MONYY7. Age:32.;
  format BMI 6.3 DOB MONYY7.;

label ID="PUBID - YTH ID CODE 1997" Gender="1= Male, 2=Female" DOB_M="Date of Birth Month" DOB_Y="Date of Birth Year" Race="1= Black, 2= Hispanic, 3= Mixed Race (Non-Hispanic) 4= Non-Black / Non-Hispanic" Year="Year the Survey took place" Height="Height in
 Inches" Weight="Weight in pounds" BMI="Body Mass Index";
datalines;
1 2 9 1981 4 1997 67 145 22.708 SEP1981 16
1 2 9 1981 4 1998 67 150 23.491 SEP1981 17
1 2 9 1981 4 1999 67 150 23.491 SEP1981 18
1 2 9 1981 4 2000 68 160 24.325 SEP1981 19
1 2 9 1981 4 2001 67 163 25.527 SEP1981 20
1 2 9 1981 4 2002 67 155 24.274 SEP1981 21
1 2 9 1981 4 2003 67 153 23.961 SEP1981 22
1 2 9 1981 4 2004 67 160 25.057 SEP1981 23
1 2 9 1981 4 2005 67 160 25.057 SEP1981 24
1 2 9 1981 4 2006 67 157 24.587 SEP1981 25
1 2 9 1981 4 2007 67 156 24.430 SEP1981 26
1 2 9 1981 4 2008 67 160 25.057 SEP1981 27
1 2 9 1981 4 2009 67 158 24.744 SEP1981 28
1 2 9 1981 4 2010 67 152 23.804 SEP1981 29
1 2 9 1981 4 2011 67 155 24.274 SEP1981 30
1 2 9 1981 4 2013 67 152 23.804 SEP1981 32
1 2 9 1981 4 2015 67 175 27.406 SEP1981 34
2 1 7 1982 2 1997 67 135 21.142 JUL1982 15
;;;;

 

Thank you for that link, it was REALLY helpful- will bookmark.

 

Im not getting errors per se, but not getting the results that I am intending. Trying to find a creative solution around the problem and wanted to see if anyone had experience with this sort of thing/could give me a hint.

 

If I havent explained well enough, I can try to explain better.

Reeza
Super User
Make this two steps. First identify all individuals who are 18 and BMI is normal. Then filter your main data set for just those IDs using a MERGE or SQL.

proc sql;
create table want as
select * from have
where id in (Select id from have where age=18 and bmi between 20 and 30);
quit;
joebacon
Pyrite | Level 9
Reeza, like always, you're a legend.

Thank you so much!

If you get to reading this, is there a big difference between doing a Merge or SQL?

No worries if not, I can look it up.
Reeza
Super User

@joebacon wrote:
Reeza, like always, you're a legend.

Thank you so much!

If you get to reading this, is there a big difference between doing a Merge or SQL?

No worries if not, I can look it up.

I find SQL much easier to understand for these type of issues than a merge. The code is also simpler.

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!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 5 replies
  • 819 views
  • 2 likes
  • 3 in conversation