BookmarkSubscribeRSS Feed
ManitobaMoose
Quartz | Level 8

 

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' ;
        Output ;
        End ;
Datalines ;
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 ;
run ;

Title "Mean Read Speeds" ;
proc means mean ;
    var Method ;
run ;

--------------------------------

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!

 

4 REPLIES 4
ManitobaMoose
Quartz | Level 8

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 @@ ;
        Output ;
    End ;
Datalines ;

Tom
Super User Tom
Super User

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.

 

 

Astounding
PROC Star

@ 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.

Reeza
Super User

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;

 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 4 replies
  • 1418 views
  • 0 likes
  • 4 in conversation