Hi. I'm new to SAS so I'd greatly appreciate any help anyone could provide me. So say, I have a data set with ~500 observations and my ultimate goal is to run ANCOVA of this data set with response variable y, factors i and j, and possible covariates k and l. I have 5 levels of i and two levels of j. Since, I first want to try to run a completely balanced ANCOVA, I don't want to use the entire data set, just the first n observations (according to ID) for each of the ten factor-level combinations.
I initially wrote a line with
IF (ID > 10*n) then delete;
before realizing how stupid that is.
How would I go about doing this?
Here's a mockup of what part of the data looks like:
ID i j k l y
1 3 1
2 3 1
3 2 1
4 4 1
5 5 2
6 1 1
7 4 2
8 2 2
9 3 1
10 5 2
11 1 1
12 3 2
13 2 1
14 1 2
15 5 1
...
So i=1, j=1, would have ID 11 as the first observation.
and i=2, j=1, would have ID 3, ID 13, etc.
It feels like this should be a pretty simple thing to do but I cannot for the life of me figure out how to do it. Would anybody be able to point me in the right direction? Thanks in advance for your help!
Hi,
There are many ways to get the first N obsevations.
Use the statement as:
IF (ID > n) then delete;
If your data set is named as MYDATA, then use;
set MYDATA(obs=n);
Hi,
Thanks for your reply. And sorry for not explaining it more clearly. I want to make it so each i-j combination has the same number of observations. If I do ID>200 or MYDATA(obs=200), it looks like it will just pull whatever the first 200 observations are. I want each combination to have 20 observations.
Yes. Now I see your requirement. It takes more time to give you solution without knowing some sample data set. Again you may want random sample in each i-j th combination. I have very limited time to give you a solution immediately. Someone will help you soon.
Refer to the following and see Example 4:
https://stats.idre.ucla.edu/sas/faq/how-can-i-take-a-stratified-random-sample-of-my-data/
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.
Ready to level-up your skills? Choose your own adventure.