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

Hello! I have a data set that I need separated into 10 observations. The data has 3 lines for each observation. part of my assignment is to separate observations by the section which is column 5 on the first line. The second two lines are HW and quiz scores. I would like to have SAS read everything as one big table, because from there I am fairly comfortable dividing the data up after that. Ive tried many things, but this is the closest Ive got:

 

data hw7;

input coursename $1-4 section 5 ID 6-7 Name $9-16 #5 a 1-3 b 4-6 c 7-9 d 10-12 e 13-15 f #6 16-18 g 1-3 h 4-6 i 7-9 j 10-12 k 13-15 l 16-18;
*input coursename $1-4 section 5@;
datalines;
M100101 Armann
10 10 10 10 8 9
13 15 10
M100102 Ellery
8 10 7 6 10 1
7 12 15
M100103 Anna
7 5 10 4 10 10
12 9 11
M100104 Tomo
9 8 10 5 10 5
10 14 12
M100301 Glenn
9 6 11 13
2 4 6 5 9 10 10 
M100302 Duc
14 12 10 12
9 10 10 8 10 10 10
M100303 Woo
14 15 12 9
8 7 9 10 5 9 10
M100304 Olson
10 10 12 11
5 10 7 9 8 10 10 
M100305 Ma
12 15 14 10
10 10 10 10 9 10 9
M100306 Garcia
14 13 15 11
9 10 8 10 10 6 10
;
run;
proc print;
title 'SAD FACE :(';
run;

At least SAS knows I have 10 observations now, but I would like sas to print line 2 as a, b, c, d, e, f and line 3 as g, h, i, j , k, l on one observation line in the final output. Please help!

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

First, your data step does not run as posted. The column assignments don't match the data actual positions.

 

Second, you should show something about what you expect the output to look like.

I'm not quite sure exactly what you expect.

 

Proc print expects to output tabular data with one observation per line.

 

I have to assume you are actually reading from an external text file. This modified data step may work a bit better

data hw7;
infile datalines truncover;
input 
   coursename $1-4 section 5 ID 6-7 Name $9-16 
   / a b  c  d  e   f  / g  h  i  j  k  l ;
datalines;
M100101 Armann
10 10 10 10 8  9
13 15 10
M100102 Ellery
8 10 7  6 10 1
7 12 15
M100103 Anna
7  5 10 4  10 10
12 9 11
M100104 Tomo
9 8 10 5  10 5
10 14 12
M100301 Glenn
9 6 11 13
2 4 6 5 9 10 10 
M100302 Duc
14 12 10 12
9 10 10 8 10 10 10
M100303 Woo
14 15 12 9
8 7 9 10 5 9 10
M100304 Olson
10 10 12 11
5 10 7 9 8 10 10 
M100305 Ma
12 15 14 10
10 10 10 10 9 10 9
M100306 Garcia
14 13 15 11
9 10 8 10 10 6 10
;
run;

the / in the Input statement basically says "continued on next line" and as long as all of your records have all three lines things will be fine for reading.

View solution in original post

3 REPLIES 3
ballardw
Super User

First, your data step does not run as posted. The column assignments don't match the data actual positions.

 

Second, you should show something about what you expect the output to look like.

I'm not quite sure exactly what you expect.

 

Proc print expects to output tabular data with one observation per line.

 

I have to assume you are actually reading from an external text file. This modified data step may work a bit better

data hw7;
infile datalines truncover;
input 
   coursename $1-4 section 5 ID 6-7 Name $9-16 
   / a b  c  d  e   f  / g  h  i  j  k  l ;
datalines;
M100101 Armann
10 10 10 10 8  9
13 15 10
M100102 Ellery
8 10 7  6 10 1
7 12 15
M100103 Anna
7  5 10 4  10 10
12 9 11
M100104 Tomo
9 8 10 5  10 5
10 14 12
M100301 Glenn
9 6 11 13
2 4 6 5 9 10 10 
M100302 Duc
14 12 10 12
9 10 10 8 10 10 10
M100303 Woo
14 15 12 9
8 7 9 10 5 9 10
M100304 Olson
10 10 12 11
5 10 7 9 8 10 10 
M100305 Ma
12 15 14 10
10 10 10 10 9 10 9
M100306 Garcia
14 13 15 11
9 10 8 10 10 6 10
;
run;

the / in the Input statement basically says "continued on next line" and as long as all of your records have all three lines things will be fine for reading.

valarievil
Obsidian | Level 7

AHHH! that / is the command I was looking for. I wonder why nothing came up when I searched for "how to tell SAS to start reading from the next line"... oh well thank you!

ballardw
Super User

@valarievil wrote:

AHHH! that / is the command I was looking for. I wonder why nothing came up when I searched for "how to tell SAS to start reading from the next line"... oh well thank you!


The INPUT statement has a lot of options and that results in sub-sets in the documentation for LIST, Named, Formatted and Column methods of reading data plus combinations. So it takes awhile to navigate all the documentation. The / documentation occurs just after the # in the base INPUT documentation though it may not be obvious what "advances the pointer to column 1 of the next input record" might mean.

 

Once upon a time your # (line input indicator) wouldn't even work unless you specifically provided an option on the INFILE statement to indicate how many rows of a source file were to be available to read from.

 

The real fun part comes when you have files that have a key value that indicates that you need to pull a value from a prior row of data and are using the Line (#) indicators with a calculated value.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 3205 views
  • 2 likes
  • 2 in conversation