A question in input statement

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 7
Accepted Solution

A question in input statement

Hi everybody! I encountered a question in the input statement. I want to create a dataset with one variable and three observations. Each observation is a single digit. 

The data step I wrote is the following:

data k;

input x 1. @@;

datalines;

123

;

run;

Those three digits are next to each other and I want them to lie in three observations. When I ran the data step, it gave a data set with 80 observations and one variable x. The first three observations are 1, 2 and 3. The rest of the observations are all missing value. You could run it if you like. Could you be so kind to tell me why it happened? It should be a dataset with variable x and three observations.

Thanks a lot!


Accepted Solutions
Solution
‎11-02-2015 09:19 AM
Super User
Super User
Posts: 6,361

Re: A question in input statement

[ Edited ]

SAS will pad the inline records ('CARDS') to 80 columns (actually a multiple of 80 if your text lines are longer than 80).

You could add a line to skip the missing values.

if x=. then delete;

 

Or you could use PARMCARDS instead of CARDS (datalines). In line data generated this way is not padded to card image length.

 

options parmcards=tempdata;
filename tempdata temp;
parmcards;
123
;
data k;
  infile tempdata;
  input x 1. @@;
run;

   

View solution in original post


All Replies
Regular Contributor
Posts: 161

Re: A question in input statement

[ Edited ]

Hello,

 

Here is a small modification of your code that should give you 3 observations of 1 variable.  I split the data into 3 lines and read it with a minor tweak..

 

 

data k;
input x:1.;
datalines;
1
2
3
;
run;

Proc print;

 Here is a print for your reference...

 


Obs	x
1	1
2	2
3	3

Hope this helps.... Good Luck...!!!

 

Kannan Deivasigamani
Occasional Contributor
Posts: 7

Re: A question in input statement

Thank you very much for your time and attention! But the thing is that I cannot change the datalines. The datalines must remain unchanged as 123. 

Regular Contributor
Posts: 161

Re: A question in input statement

Is there a delimiter? and how long is the data?

Kannan Deivasigamani
Occasional Contributor
Posts: 7

Re: A question in input statement

No. The digits are just next to each other. The data contains 50 sigle digits which are next to each other. And I am confused at the outcome of my data step. It should work.
Occasional Contributor
Posts: 7

Re: A question in input statement

Thank you very much for your time and attention! But the thing is that I cannot change the datalines. The datalines must remain unchanged as 123.
Grand Advisor
Posts: 10,215

Re: A question in input statement

If you always know how many you are reading then @ instead of @@

data k;
   do i=1 to 3;
   input x 1. @;
   output;
   end;
   drop i;
datalines;
123
;
run;
Solution
‎11-02-2015 09:19 AM
Super User
Super User
Posts: 6,361

Re: A question in input statement

[ Edited ]

SAS will pad the inline records ('CARDS') to 80 columns (actually a multiple of 80 if your text lines are longer than 80).

You could add a line to skip the missing values.

if x=. then delete;

 

Or you could use PARMCARDS instead of CARDS (datalines). In line data generated this way is not padded to card image length.

 

options parmcards=tempdata;
filename tempdata temp;
parmcards;
123
;
data k;
  infile tempdata;
  input x 1. @@;
run;

   

Occasional Contributor
Posts: 7

Re: A question in input statement

Thanks a lot! But I still have a question. If this is the case, why I can't see another missing values if the data step is like this:
data k;
input x @@;
datalines;
1 2 3
;
run;
Much appreciate your attention and time!
Regular Contributor
Posts: 161

Re: A question in input statement

1. You have introduced a space between the individual records in this scenario. I guess it is read as a delimited data.

 

2. You have also removed the variable type definition of x. 

 

Perhaps the gurus here may add to it.

Kannan Deivasigamani
Occasional Contributor
Posts: 7

Re: A question in input statement

Yeah. That, I think, should be the reason. Thank you for your help! Much appreicate your attention! 

Frequent Contributor
Posts: 108

Re: A question in input statement

try this

data k;
input x1$;
do i = 1 to 3;
Dig1 = substr(x1,i,1);
output;
end;
drop i;
datalines;
123
456
675
;
run;
Proc Print data = k;
run;

 

 

Occasional Contributor
Posts: 7

Re: A question in input statement

Thank you very much for your help! The problem has been solved now!
☑ This topic is SOLVED.

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

Discussion stats
  • 12 replies
  • 616 views
  • 5 likes
  • 5 in conversation