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

Hey All,

I am trying to run this code using datalines infile below to give an output data with 4 observations and 10 observations. However when I run, I am receiving either an incorrect output datatable and I don't know what I am doing wrong even though I am pretty sure it must be a simple issue to fix.

 

The datalines has data deliminated by a single space.

 

I know datalines default deliminator is a space so why isn't it reading the deliminators correctly but instead reading two observations from the datalines into a single observation in the output table? I shouldn't have to tell SAS the length of the variable because deliminators are consistent so what's going on?

Please help me!

 

What I want:

ELEMSCHOOL      SEXCD     STUDENT    PLACE

WASHINGTON      F                LIZ               2

LINCOLN               F                MARIA         3

...
WASHINGTON      M               DONALD      4

 

*ELEMSCHOOL SEXCD STUDENT are character variables, PLACE is a numeric variable*;

 

What I get:

ELEMSCHOOL      SEXCD     STUDENT    PLACE

WASHINGTON      F                .                    .

LINCOLN M           L                LINCOLN      .

*Output only gives two observations*;

 

Here is code I have tried:

First Try:

 

DATA WORK.RESULTS;
INFILE DATALINES;
INPUT ELEMSCHOOL :$10.
SEXCD :$1.
STUDENT :$8.
PLACE 2.;
DATALINES;
Washington F Liz 2
Lincoln F Maria 3
Washington M Michael 10
Washington M Charles 8
Lincoln M Michael 1
Lincoln M George 6
Lincoln F Susan 5
Lincoln M Richard 7
Lincoln F Margaret 9
Washington M Donald 4
;

 

Second Try:

 

DATA WORK.RESULTS;
INFILE DATALINES;
INPUT ELEMSCHOOL $
SEXCD $
STUDENT $
PLACE ;
DATALINES;
Washington F Liz 2
Lincoln F Maria 3
Washington M Michael 10
Washington M Charles 8
Lincoln M Michael 1
Lincoln M George 6
Lincoln F Susan 5
Lincoln M Richard 7
Lincoln F Margaret 9
Washington M Donald 4
;

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

@Deanna_Payne wrote:

Thanks to everyone for their help. I figured out why perfectly good code wasn't working on my SAS studio. I had mixed tabs and spaces which created unequal columns which is why I kept getting confused output tables.

 

Best,

D


 You should not be putting physical tab characters into your program files, use spaces to indent your lines of code. The SAS editors have an option to replace tabs with spaces.  Example from SAS/Studio 

image.png

View solution in original post

4 REPLIES 4
Reeza
Super User

Your second try worked fine, except that the first variable was truncated because the default length of a variable is 8. 

Are you sure your delimiter is a space?

 

DATA WORK.RESULTS;
INFILE DATALINES;
length elemschool $12. sexcd $1.;
INPUT ELEMSCHOOL $
SEXCD $
STUDENT $
PLACE ;
DATALINES;
Washington F Liz 2
Lincoln F Maria 3
Washington M Michael 10
Washington M Charles 8
Lincoln M Michael 1
Lincoln M George 6
Lincoln F Susan 5
Lincoln M Richard 7
Lincoln F Margaret 9
Washington M Donald 4
;

@Deanna_Payne wrote:

Hey All,

I am trying to run this code using datalines infile below to give an output data with 4 observations and 10 observations. However when I run, I am receiving either an incorrect output datatable and I don't know what I am doing wrong even though I am pretty sure it must be a simple issue to fix.

 

The datalines has data deliminated by a single space.

 

I know datalines default deliminator is a space so why isn't it reading the deliminators correctly but instead reading two observations from the datalines into a single observation in the output table? I shouldn't have to tell SAS the length of the variable because deliminators are consistent so what's going on?

Please help me!

 

What I want:

ELEMSCHOOL      SEXCD     STUDENT    PLACE

WASHINGTON      F                LIZ               2

LINCOLN               F                MARIA         3

...
WASHINGTON      M               DONALD      4

 

*ELEMSCHOOL SEXCD STUDENT are character variables, PLACE is a numeric variable*;

 

What I get:

ELEMSCHOOL      SEXCD     STUDENT    PLACE

WASHINGTON      F                .                    .

LINCOLN M           L                LINCOLN      .

*Output only gives two observations*;

 

Here is code I have tried:

First Try:

 

DATA WORK.RESULTS;
INFILE DATALINES;
INPUT ELEMSCHOOL :$10.
SEXCD :$1.
STUDENT :$8.
PLACE 2.;
DATALINES;
Washington F Liz 2
Lincoln F Maria 3
Washington M Michael 10
Washington M Charles 8
Lincoln M Michael 1
Lincoln M George 6
Lincoln F Susan 5
Lincoln M Richard 7
Lincoln F Margaret 9
Washington M Donald 4
;

 

Second Try:

 

DATA WORK.RESULTS;
INFILE DATALINES;
INPUT ELEMSCHOOL $
SEXCD $
STUDENT $
PLACE ;
DATALINES;
Washington F Liz 2
Lincoln F Maria 3
Washington M Michael 10
Washington M Charles 8
Lincoln M Michael 1
Lincoln M George 6
Lincoln F Susan 5
Lincoln M Richard 7
Lincoln F Margaret 9
Washington M Donald 4
;


 

sjbothwell
Calcite | Level 5

Hi Deanna, 

 

Your first try is almost correct too! If you change '2.' to ':2.' for your place variable your code should run correctly. In general, the colon format modifier is nice to use when importing data, whether the variable is character or numeric. 

 

DATA WORK.RESULTS;
INFILE DATALINES;
INPUT ELEMSCHOOL :$10.
SEXCD :$1.
STUDENT :$8.
PLACE 2.;
DATALINES;
Washington F Liz 2
Lincoln F Maria 3
Washington M Michael 10
Washington M Charles 8
Lincoln M Michael 1
Lincoln M George 6
Lincoln F Susan 5
Lincoln M Richard 7
Lincoln F Margaret 9
Washington M Donald 4
;

Deanna_Payne
Obsidian | Level 7

Thanks to everyone for their help. I figured out why perfectly good code wasn't working on my SAS studio. I had mixed tabs and spaces which created unequal columns which is why I kept getting confused output tables.

 

Best,

D

Tom
Super User Tom
Super User

@Deanna_Payne wrote:

Thanks to everyone for their help. I figured out why perfectly good code wasn't working on my SAS studio. I had mixed tabs and spaces which created unequal columns which is why I kept getting confused output tables.

 

Best,

D


 You should not be putting physical tab characters into your program files, use spaces to indent your lines of code. The SAS editors have an option to replace tabs with spaces.  Example from SAS/Studio 

image.png

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
  • 4 replies
  • 831 views
  • 1 like
  • 4 in conversation