DATA Step, Macro, Functions and more

Formatted Input why are the two results not equivalent?

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 119
Accepted Solution

Formatted Input why are the two results not equivalent?

Hi,

 

In the first code, the two missing values are properly displayed, but not in the second. Why not?

 

Below is the raw data:

 

90 80 98
78 88
65 66 69
92 94 96
78    79

First code.

Libname Learn'/folders/myfolders/Learn' ;

Data Problem_21_4 ; 
	infile '/folders/myfolders/Learn/Scores_List.sas' truncover ;   /*or Missover*/
	input (Score1-Score3) (2. + 1) ; 
run ; 	
	
proc print data=Problem_21_4 noobs ; 
run ; 

First Code results:

 

Score1 	Score2 	Score3
90 	80 	98
78 	88 	.
65 	66 	69
92 	94 	96
78 	. 	79

Second Code

 

Libname Review'/folders/myfolders/Review' ;
Libname Learn'/folders/myfolders/Learn' ; 

data review.Prob21_4 ;
	infile '/folders/myfolders/Learn/Scores_List.sas' truncover ; 
	input @1 Score1 : 2.
		  @4 Score2 : 2.
		  @7 Score3 : 2. ;
run ; 

proc print data=review.Prob21_4 ; 
run ; 

Second Code Results

 

Obs 	Score1 	Score2 	Score3
1 	90 	80 	98
2 	78 	88 	.
3 	65 	66 	69
4 	92 	94 	96
5 	78 	79 	79

 


Accepted Solutions
Solution
Thursday
Super User
Posts: 6,542

Re: Formatted Input why are the two results not equivalent?

Posted in reply to ManitobaMoose

While @novinosrin has the right "fix" to the problem, I think your question isn't how to fix it.  It looks like the question is, "Why does this happen?"

 

Since removing the colon impacts the results, it must be having an impact on how SAS reads the data.  More specifically, the colon means, "Scan across the data from left to right.  Once you find something, apply the instructions (2. in this case)."

 

So on the 5th line of data, SAS begins at column 4 and starts looking for a value.  It doesn't find anything until it gets to column 7 where it finds "79" (in columns 7 and 8).  So column 7 is where the INPUT statement starts applying the instructions = 2. = read the next two characters to get a value for SCORE2.

View solution in original post


All Replies
PROC Star
Posts: 1,339

Re: Formatted Input why are the two results not equivalent?

Posted in reply to ManitobaMoose

Get rid of the colon format modifier if you are using formatter input and NOT modified list input like this in your 2nd to work

 

data Prob21_4 ;
	infile cards truncover ; 
	input @1 Score1  2.
		  @4 Score2  2.
		  @7 Score3  2. ;
cards;
90 80 98
78 88
65 66 69
92 94 96
78    79
;
run ; 
Solution
Thursday
Super User
Posts: 6,542

Re: Formatted Input why are the two results not equivalent?

Posted in reply to ManitobaMoose

While @novinosrin has the right "fix" to the problem, I think your question isn't how to fix it.  It looks like the question is, "Why does this happen?"

 

Since removing the colon impacts the results, it must be having an impact on how SAS reads the data.  More specifically, the colon means, "Scan across the data from left to right.  Once you find something, apply the instructions (2. in this case)."

 

So on the 5th line of data, SAS begins at column 4 and starts looking for a value.  It doesn't find anything until it gets to column 7 where it finds "79" (in columns 7 and 8).  So column 7 is where the INPUT statement starts applying the instructions = 2. = read the next two characters to get a value for SCORE2.

Frequent Contributor
Posts: 119

Re: Formatted Input why are the two results not equivalent?

Posted in reply to Astounding

Thanks. That is  correct. I wanted to know why it didn't work. Now I better understand the colon in this context. Thanks.

☑ This topic is solved.

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

Discussion stats
  • 3 replies
  • 112 views
  • 0 likes
  • 3 in conversation