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

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!

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

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

3 REPLIES 3
PGStats
Opal | Level 21

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
Therain
Calcite | Level 5
Thank you! It works!
Doc_Duke
Rhodochrosite | Level 12

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-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
  • 3 replies
  • 2575 views
  • 0 likes
  • 3 in conversation