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

I am working on an exercise in which I believe I need to use triailing @.

I am given this data:

 

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

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

 

The bolded number is called the course section.

If course section = 1 then print quizgrades hwgrades

If course section = 3 then print hw grades quizgrades

 

I need to print out two datasets, one for section 1 and one for section 3.

I am having an issue with my trailing @. I am not sure what I am doing wrong or how I can fix this.

(using datalines for simplicity for now, will change later)

Here's my code and the output

 

 *libname datain '\\Client\I$\SAS2018\Data';

data hw7_a;
*infile '\\Client\I$\SAS2018\Data\studentinfo.txt';
input Name $1-4 Section 5 @ Student_Chars 7;
if section= 1 then do;
        input Q1-Q6 2 HW1-HW3 2;
        output;
        end;
if section= 3 then do;
        input HW1-HW4 2 Q1-Q7 2;
        output;
        end;
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

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
;
run;

proc print;
format Student_Chars z2.;
run;

Screen Shot 2018-12-07 at 10.43.40 AM.png

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Actually you do not want to use the @ to hold position. You want to read the first of the three lines. Then depending on result for the first line read two additional lines. Your @ was not in the correct place to hold the line for reading, it should be the last character on the input statement. The @ may also be used to indicate a text position ( @'M100' for example) to read or a column number (@27) to move the input pointer.

 

Please see:

data hw7_a;
*infile '\\Client\I$\SAS2018\Data\studentinfo.txt';
input Name $1-4 Section 5 Student $ ;
if section= 1 then do;
        input  Q1-Q6 
             / HW1-HW3;
        output;
        end;
if section= 3 then do;
        input HW1-HW4 
             / Q1-Q7 ;
        output;
        end;
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
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
;
run;

The / in the input statement says to continue reading on the next line. Datalines with blank lines don't always work the same as a text file so I removed them from the example. If your data file actually has that blank after every group of three lines add another / to the section input statements after the last variable.

 

 

 

With spaces between values you can use LIST input without trying to force a position or number of columns for a numeric value.

You also did not provide anything where the code knew that the Student was to be a character variable. The $ does that with a default length of 8 characters.

 

I don't know what you mean exactly by "print two datasets" but you can get different output for each section using the example code since the records are in Section order  with:

proc print  data=hw7_a;
 by section;
run;

If your data actually has Section values scattered then you would sort the data before printing with proc sort.

 

There are different ways to get print output such as different title statements and where statement in the Proc print to restrict a specific print to selected values of the section variable. I can't tell if you actually want the order of variables the same or different in the print results.

Or a custom format for the section variable could show text in the By line (Section 😃 of

View solution in original post

2 REPLIES 2
ballardw
Super User

Actually you do not want to use the @ to hold position. You want to read the first of the three lines. Then depending on result for the first line read two additional lines. Your @ was not in the correct place to hold the line for reading, it should be the last character on the input statement. The @ may also be used to indicate a text position ( @'M100' for example) to read or a column number (@27) to move the input pointer.

 

Please see:

data hw7_a;
*infile '\\Client\I$\SAS2018\Data\studentinfo.txt';
input Name $1-4 Section 5 Student $ ;
if section= 1 then do;
        input  Q1-Q6 
             / HW1-HW3;
        output;
        end;
if section= 3 then do;
        input HW1-HW4 
             / Q1-Q7 ;
        output;
        end;
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
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
;
run;

The / in the input statement says to continue reading on the next line. Datalines with blank lines don't always work the same as a text file so I removed them from the example. If your data file actually has that blank after every group of three lines add another / to the section input statements after the last variable.

 

 

 

With spaces between values you can use LIST input without trying to force a position or number of columns for a numeric value.

You also did not provide anything where the code knew that the Student was to be a character variable. The $ does that with a default length of 8 characters.

 

I don't know what you mean exactly by "print two datasets" but you can get different output for each section using the example code since the records are in Section order  with:

proc print  data=hw7_a;
 by section;
run;

If your data actually has Section values scattered then you would sort the data before printing with proc sort.

 

There are different ways to get print output such as different title statements and where statement in the Proc print to restrict a specific print to selected values of the section variable. I can't tell if you actually want the order of variables the same or different in the print results.

Or a custom format for the section variable could show text in the By line (Section 😃 of

clancaster
Fluorite | Level 6

This was incredibly insightful. Thank you.

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!

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