08-24-2017 04:51 PM - last edited on 08-24-2017 05:22 PM by Tom
This post is really about the @, athough while I am already posting, I might as well ask about the proc means statement, which I am just learning. l am learning from "Learning SAS by Example". There is no solution for this problem in the book, and I do not know why this is not working. Here is the code:
Data ReadSpd ;
Input Score @ ;
Do Method = 'A', 'B', 'C' ;
250 255 256 300 244 268 301 322 256 333
267 275 256 320 250 340 345 290 280 300
350 350 340 290 377 401 380 310 299 399
proc print Data=ReadSpd ;
Title "Mean Read Speeds" ;
proc means mean ;
var Method ;
1. The proc print statement only gives me 9 readings, not 30, as it should. When I change the code to read input Score @@ I get 90 readings. Why?
2. How do I get the means for each of the "Speed Readers", which are 'A', 'B'. and 'C'? I want to know what the mean score was for each of the speed readers using the proc means statement.
Thanks a lot for your help!
08-24-2017 05:06 PM
For some reason, when I rearranged the location of the input text to be within the Do statement, and used @@, now I get 30 readings. I guess the input variables must be within the DO statement?
Do Method = 'A', 'B', 'C' ;
Input Score @@ ;
08-24-2017 05:20 PM
The INPUT statement is executable, so where you place it relative to the DO loop makes a big difference. The original code
input score @ ; do method = 'A', 'B', 'C' ; output ; end ;
Is saying read the first value from the line and hold the line for this iteration of the data step. Then output it three times with different values of METHOD. So you only ever read the first score on each line.
Your new code
do method = 'A', 'B', 'C' ; input score @@; output ; end ;
Is telling it to set METHOD to different vlaues and then read the next score from the input and hold the line even across data step interations. So for each pass of the data step you will read three values and assign them to different methods.
08-24-2017 05:18 PM
@ holds the line you are reading from temporarily, until you reach the end of the statements in the DATA step.
@@ holds the line you are reading from permanently, regardless of whether you reach the end of the statements in the DATA step. The only way to get to the next line is for the INPUT statement to search for another SCORE and, not finding one on the current line, moves on to the next line.
Using all the programs you have tested, you should be able to see this at work. Look at more than the number of observations. Look at the values of the variables in your output data set and you can see these principles at work.
08-24-2017 05:29 PM
Re #2 - you should preferably only ask one question at a time.
Look at the CLASS or BY statement.
If you haven't come across the BY statement I suggest you read up on it, it's an incredibly powerful feature in SAS that's worth learning how to use.
proc sort data=sashelp.class out=class; by sex; run; proc means data=class; by sex; var weight height; run;