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!
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;
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...!!!
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.
Is there a delimiter? and how long is the data?
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;
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;
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.
Yeah. That, I think, should be the reason. Thank you for your help! Much appreicate your attention!
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;
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.