BookmarkSubscribeRSS Feed
melissagodfrey
Fluorite | Level 6

Hi all,

I just finished lesson 5 level 2 practice and getting the correct responses with no syntax errors. However, upon comparing my syntax, it is different than the proposed answer. Can someone please help me understand why you would use the proposed answer over the syntax I provide? (I feel like I am getting a bit confused as the practices sometimes have us try new syntax, meanwhile, I am using syntax that was just practiced earlier in the session and I am not sure how the new syntax is different than the syntax I wrote?) 

 

Practice Steps are:

Create a new program. Write a PROC MEANS step to analyze rows from pg1.np_westweather with the following specifications:

  • Exclude the rows where values for Precip are notequal to 0.
  • Analyze precipitation amounts grouped by Nameand Year.
  • Create only an output table, named rainstats, with columns for the N and SUM statistics.
  • Name the columns RainDays and TotalRain, respectively.
  • Keep only those rows that are the combination of Year and Name.
  • Submit the program and view the output data.

 

My answer is below:
proc means data=PG1.NP_WESTWEATHER; where Precip =0; class name year; ways 2; output out=rainstats N=RainDays sum=TotalRain; run;
Proposed Answer:
proc means data=pg1.np_westweather noprint;
    where Precip ne 0;
    var Precip;
    class Name Year;
    output out=rainstats(where=(_type_=3))
           n=RainDays sum=TotalRain;
run;
3 REPLIES 3
Reeza
Super User
where Precip =0; 

is not equal to this:

 

    where Precip ne 0;

Which is your biggest difference. 

 

Because they said:

  • Keep only those rows that are the combination of Year and Name.

I suspect that's why they used the data set option of WHERE. WAYS is another way to do this and I can think of at least two more ways to accomplish this within PROC MEANS - NWAY option and TYPES statements. 

 

ballardw
Super User

Your solution

My answer is below:

proc means data=PG1.NP_WESTWEATHER;
where Precip =0;      
class name year;
ways 2;
output out=rainstats N=RainDays sum=TotalRain;
run;

has the potential to get a different result because without a VAR statement which variable gets stuck into totalrain may be problematic.

 

Compare the results from these two snippets:

proc means data=SASHELP.Class;
   class sex ;
   ways 1;
   output out=work.summ1 N=count sum=total;
run;

proc means data=SASHELP.Class;
   class sex;
   ways 1;
   var weight;
   output out=work.summ2 N=count sum=total;
run;

The variable summarized in the first code is the FIRST numeric not used in class variable(if any class variables are numeric).

 

The second summarizes a specific variable.

When the data has few variables you may get lucky and get the same answer as needed but if the order of variables is other than expected, or not considered prior to writing the code, then you get interesting results.

Note that a frequently helpful feature of Proc Means / summary is autoname to name output variables:

proc means data=SASHELP.Class;
   class sex ;
   ways 1;
   output out=work.summ3 n= sum= mean= / autoname;
run;

Which will create the count, sum and mean of all numeric variables (not used on class, freq or weight statements) and appends the statistic to the variable name.

 

Reeza
Super User
One more difference - NOPRINT. Since you’re creating a data set you don’t need the displayed output and it’s different than the data set so it can be confusing for a new programmer.
Also, generating the output takes up more time than you’d expect.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
LIBNAME 101

Follow along as SAS technical trainer Dominique Weatherspoon expertly answers all your questions about SAS Libraries.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 3824 views
  • 6 likes
  • 3 in conversation