DATA Step, Macro, Functions and more

"If...then...delete;" issue

Accepted Solution Solved
Reply
Contributor
Posts: 27
Accepted Solution

"If...then...delete;" issue

I want to run a PROC REG on some data with many variables. However, some of the variables were collected at the plot level while others were collected in transects within the plot. If i take the mean of the transect variables, that gives me a plot level variable that i can then use the PROC REG function on. However, if i use PROC MEANS to get the mean variable of these transect data points it also gives me N, MAX, MIN, and STD output. If i run PROC REG on the data generated by PROC MEANS it includes the 4 variables i mentioned in the regression, which i obviously don't want. So, i want to delete those 4 variables and have PROC REG use the MEAN variable exclusively.

proc means data=AllEvents;
class plotID;
var logN2O logVWC Soil_N Soil_C SoilC_N LitterN LitterC LitterC_N pH_H2O pH_CaCl2 logNH4 logTN;
output out=plotmeans;
run;

data plotmeans;
if "_STAT_"="N" then delete;
if "_STAT_"="MAX" then delete;
if "_STAT_"="MIN" then delete;
if "_STAT_"="STD" then delete;
output;
run;

 

The proc means command works fine but the my "if...then...delete;" command doesn't work. It doesn't generate an error, but it does give me the message:  NOTE: Variable _STAT_ is uninitialized.

How can i generate an output of the mean of the variables i'm interested in at the plot level that i can then run PROC REG with?


Accepted Solutions
Solution
‎10-13-2017 03:14 PM
Super User
Super User
Posts: 7,860

Re: "If...then...delete;" issue

If you just want PROC MEANS to generate the MEAN then tell it that.  If you add MEAN= to the OUTPUT statement then SAS will store the means into the variables with the same names as the input variables.

proc means data=AllEvents;
  class plotID;
  var logN2O logVWC Soil_N Soil_C SoilC_N
      LitterN LitterC LitterC_N pH_H2O pH_CaCl2 logNH4 logTN
  ;
  output out=plotmeans mean=;
run;

Your data step has a number of problems. First you are not telling it what data to read. Second you are comparing two string literals in your IF statements that can never be equal.  You probably could have gotten it to work with code like this just use a subsetting IF instead of IF/THEN/DELETE.

data plotmeans;
  set plotmeans;
  if _stat_='MEAN';
run;

View solution in original post


All Replies
Super User
Posts: 22,875

Re: "If...then...delete;" issue

You can control the output in various ways. Look at the OUTPUT statement documentation for starters to select only the MEAN. 

And then look at the STACKODS option to see if you want a different structure. 

 

proc means ...;
....

output out=want mean = /autoname;
run;

or 

 

proc means data=...  MEAN STACKODS;
....
ods output summary=want_stacked;
run;
SAS Super FREQ
Posts: 496

Re: "If...then...delete;" issue

You need a set statement after the data statement:

 

set plotmeans;

 

It reads the input data set into the data step.

Solution
‎10-13-2017 03:14 PM
Super User
Super User
Posts: 7,860

Re: "If...then...delete;" issue

If you just want PROC MEANS to generate the MEAN then tell it that.  If you add MEAN= to the OUTPUT statement then SAS will store the means into the variables with the same names as the input variables.

proc means data=AllEvents;
  class plotID;
  var logN2O logVWC Soil_N Soil_C SoilC_N
      LitterN LitterC LitterC_N pH_H2O pH_CaCl2 logNH4 logTN
  ;
  output out=plotmeans mean=;
run;

Your data step has a number of problems. First you are not telling it what data to read. Second you are comparing two string literals in your IF statements that can never be equal.  You probably could have gotten it to work with code like this just use a subsetting IF instead of IF/THEN/DELETE.

data plotmeans;
  set plotmeans;
  if _stat_='MEAN';
run;
☑ This topic is solved.

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

Discussion stats
  • 3 replies
  • 489 views
  • 2 likes
  • 4 in conversation