DATA Step, Macro, Functions and more

SAS acting weird when reading data

Accepted Solution Solved
Reply
New Contributor
Posts: 3
Accepted Solution

SAS acting weird when reading data

I am a beginner and I was trying input styles for practice. I wrote the following code:

 

data test;
/*	input name $ age Salary comma7. test1-test4 2.;*/
	input name $ age Salary comma7. test1 2. test2 2. test3 2. test4 2.;
    datalines;
abc 15 100,000 10 11 12 13
xyz 16 120,000 13 11 12 12
        ;
run;
proc print data = test;
run;

 

As expected, i get the following code

Result1

However, on running the code using the second input statement (which is commented out in the code), I get the following result:

 

Result2.JPG

 

As per my knowledge, both the input statements must be synonymous, then how can they give different results?

 

The SAS version I'm using is 9.2. Can anybody throw some light on why SAS is acting so weird. 

 

 

 


Accepted Solutions
Solution
‎03-11-2016 06:29 AM
Super User
Super User
Posts: 7,942

Re: SAS acting weird when reading data

Posted in reply to karan51290

Hi,

 

Well, SAS isn't acting wierd.  In your first example:

input name $ age Salary comma7. test1 2. test2 2. test3 2. test4 2.;

This is reading formatted input, so it reads the first string up to the space, then it reads the comma7 value.  But then for each of the subsequent test variables you are moving the pointer:

100,000  10 11 12 13

           ^

           pointer is here after comma read

So, the test1 2. statment tells SAS to read two characters from where the pointer is, this is " 1", and conert that to number.  The pointer is then:

100,000  10 11 12 13

              ^

              pointer is here after test1 read

So the next statement test2 2., reads two characters "0 " from the line.  And so on.  Hence your first example output is incorrect.  Each input statement using formatted input would need to have 3. i.e. read the space, and the two decimals, and convert to number.  

In the commented out code the 2. is only applied to the last test = test4, hence works correctly as test1 takes the string " 10", test2 " 11" etc.

 

View solution in original post


All Replies
Solution
‎03-11-2016 06:29 AM
Super User
Super User
Posts: 7,942

Re: SAS acting weird when reading data

Posted in reply to karan51290

Hi,

 

Well, SAS isn't acting wierd.  In your first example:

input name $ age Salary comma7. test1 2. test2 2. test3 2. test4 2.;

This is reading formatted input, so it reads the first string up to the space, then it reads the comma7 value.  But then for each of the subsequent test variables you are moving the pointer:

100,000  10 11 12 13

           ^

           pointer is here after comma read

So, the test1 2. statment tells SAS to read two characters from where the pointer is, this is " 1", and conert that to number.  The pointer is then:

100,000  10 11 12 13

              ^

              pointer is here after test1 read

So the next statement test2 2., reads two characters "0 " from the line.  And so on.  Hence your first example output is incorrect.  Each input statement using formatted input would need to have 3. i.e. read the space, and the two decimals, and convert to number.  

In the commented out code the 2. is only applied to the last test = test4, hence works correctly as test1 takes the string " 10", test2 " 11" etc.

 

New Contributor
Posts: 3

Re: SAS acting weird when reading data

ohh. Thanks for the clarification. Another quick question: How do I apply informats to all the variables when using range notation (test1-test4)?
Super User
Super User
Posts: 7,942

Re: SAS acting weird when reading data

Posted in reply to karan51290

I am not able to test right now but:

informat test1-test4 <format>;

should work so long, it will use the informat on all variables which appear in sequence left to right from test1 to test4.  If the ones you want are not in order, but they all use the same prefix then:

informat test: <format>;

Otherwise you need to specify each one. 

New Contributor
Posts: 3

Re: SAS acting weird when reading data

I just found out after a bit of googling, the correct way to assign same informats to all variables is like this:

 

input (score1-score5) (4. 4. 4. 4. 4.);

or a shorter form would be: 

input (score1-score5) (4.);

 

 

Additional information is here:

http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000148073.htm

 

I tried testing it and it works as expected.

 

Again, thank you for pointing me to right direction Smiley Happy

 

 

☑ This topic is solved.

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

Discussion stats
  • 4 replies
  • 296 views
  • 1 like
  • 2 in conversation