Hi,
I use SAS 9.4 and I have a dataset with a variable called "mdd", which has 1870 observations. I hope to get a descriptive statistics of mdd with 2010 observations (observation 1871-2010 will be assigned values of 0). I used the following code:
PROC UNIVARIATE DATA=have; VAR mdd; RUN;
But it only offers info about the 1870 observations.
So I used the following codes to add rows:
proc sql; insert into have set mdd=0
set mdd=0
...
set mdd=0;
This is troublesome and I add observations to 2019 by accident. Now I want to delete the observations from 2011-2019 but I do not know how to do that. So is there a code to directly add mdd=0 from observation 1871 to 2010? Also, is there a code to delete observations in a range?
Thanks!
Looks like all you need to do is extend a dataset with blank observations. You could use something like this:
data want;
set have end=done;
output;
if done then do;
mdd = 0;
do i = 1871 to 2010; output; end;
end;
drop i;
run;
(untested)
Looks like all you need to do is extend a dataset with blank observations. You could use something like this:
data want;
set have end=done;
output;
if done then do;
mdd = 0;
do i = 1871 to 2010; output; end;
end;
drop i;
run;
(untested)
As you have learned the hard way, it is good not to modify the original data. Now that you have done it, deleting the extra observations is a simple data step:
DATA haveless;
SET have;
IF _n_>2010 THEN DELETE; *Assumes added rows at the end;
RUN;
If you still have the original data, a better approach to augmentation might be:
DATA havemdd0;
DO _I_ =1871 to 2010;
mdd=0;
OUTPUT;
END;
DROP _i_;
RUN;
DATA have2;
SET have havemdd0;
RUN;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.