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

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!

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

12 REPLIES 12
kannand
Lapis Lazuli | Level 10

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
liqi0318
Calcite | Level 5

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. 

kannand
Lapis Lazuli | Level 10

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

Kannan Deivasigamani
liqi0318
Calcite | Level 5
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.
liqi0318
Calcite | Level 5
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.
ballardw
Super User

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;
Tom
Super User Tom
Super User

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;

   

liqi0318
Calcite | Level 5
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!
kannand
Lapis Lazuli | Level 10

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
liqi0318
Calcite | Level 5

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

pearsoninst
Pyrite | Level 9

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;

 

 

liqi0318
Calcite | Level 5
Thank you very much for your help! The problem has been solved now!

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
  • 12 replies
  • 1407 views
  • 5 likes
  • 5 in conversation