DATA Step, Macro, Functions and more

How to add and delete observations within a range

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 17
Accepted Solution

How to add and delete observations within a range

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!


Accepted Solutions
Solution
‎07-14-2016 02:34 PM
Respected Advisor
Posts: 4,649

Re: How to add and delete observations within a range

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)

PG

View solution in original post


All Replies
Solution
‎07-14-2016 02:34 PM
Respected Advisor
Posts: 4,649

Re: How to add and delete observations within a range

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)

PG
Occasional Contributor
Posts: 17

Re: How to add and delete observations within a range

Thank you! It works!
Trusted Advisor
Posts: 2,113

Re: How to add and delete observations within a range

[ Edited ]

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;

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 316 views
  • 0 likes
  • 3 in conversation