BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
karan51290
Fluorite | Level 6

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. 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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

4 REPLIES 4
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

 

karan51290
Fluorite | Level 6
ohh. Thanks for the clarification. Another quick question: How do I apply informats to all the variables when using range notation (test1-test4)?
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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. 

karan51290
Fluorite | Level 6

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 🙂

 

 

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
  • 1087 views
  • 1 like
  • 2 in conversation