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

tianerhu_0-1619988335094.pngtianerhu_1-1619988346065.pngtianerhu_2-1619988357278.png

Can we read the data of the variable Department from @25 ?

1 ACCEPTED SOLUTION

Accepted Solutions
SASKiwi
PROC Star

I suspect the use of a proportional font is misaligning the data under the column ruler. If StartDate does start in column 15 and takes up 10 columns then Department  can correctly start in column 25. 

View solution in original post

8 REPLIES 8
SASKiwi
PROC Star

I suspect the use of a proportional font is misaligning the data under the column ruler. If StartDate does start in column 15 and takes up 10 columns then Department  can correctly start in column 25. 

tianerhu
Pyrite | Level 9

Thank you for your help.

ballardw
Super User

I would be very careful with any training material (an assumption on my part) that ever shows log text using proportional fonts.

 

If there are diagnostic characters, such as the ruler or underscore charters that indicate the position of an error the results are not easy to read and indicates poor preparation by the publisher. i.e. they don't know that logs rely on character positions which would make me question how well they know the other material.

Sajid01
Meteorite | Level 14

Hello
I saw this question at atleast two sites online. and the question posed is 
"Which SAS informat correctly completes the program?" and the given answer is mmddyy10.

The question is properly formatted, but there is space between day, month and year.

Sajid01_1-1620050047257.png

So for the given answer to be true, the employee data file should look like this.

Sajid01_0-1620049730016.png

Thus the question needs correction.

@tianerhu may have a different source for this question, but the error essentially remains the same.

 

ballardw
Super User

@Sajid01 wrote:

Hello
I saw this question at atleast two sites online. and the question posed is 
"Which SAS informat correctly completes the program?" and the given answer is mmddyy10.

The question is properly formatted, but there is space between day, month and year.

Sajid01_1-1620050047257.png

So for the given answer to be true, the employee data file should look like this.

Sajid01_0-1620049730016.png

Thus the question needs correction.

@tianerhu may have a different source for this question, but the error essentially remains the same.

 


The spaces are acceptable when using fixed column data. Example:

data junk;
  input x ddmmyy10.;
  format x date9.;
datalines;
 2 10 2021
;

Abbreviated but in this case x is being read from all 10 columns and gets 02OCT2021 correctly. Which is why the PICTURE of the Ruler has to be accurate, the code shown says the date value is to be read starting at column 15 but the picture the OP shows does not show proper alignment for the stated informat to work. The first 2 would have to appear under the - after the + between 10 and 20 (the + then being column 15). Column 25, where the variable Department is supposed to start appears to be in the middle of the date values in picture shown. Which is why I would be very hesitant to use the version of the examples provided if the OP is showing a good copy of the material.

An approach that uses LIST format such as the following will interpret the space as delimiter between variables and fail as expected.

data junk;
  input x :ddmmyy10.;
  format x date9.;
datalines;
 2 10 2021
;
/*or*/
data junk;
  informat x ddmmyy10.;
  input x ;
  format x date9.;
datalines;
 2 10 2021
;

Remember there are 4 different types of INPUT and how they can be mixed on an input statement is truly one of SAS's strengths.

Sajid01
Meteorite | Level 14

Thank you so much  @ballardw for the wonderful explanation.
There is so much I need to learn. 
Looks like there is an error in my code.
Can you please help me understand where I am going wrong. My code and operative portion of the log are reproduced  below.

/*My code */
data employee;
infile "/home/testuser/employee2.txt";
Retain FirstName StartDate Department;
informat StartDate ddmmyy10.;
format startDate date9.;
input @1  FirstName $
      @15 StartDate 
      @25 Department $
      ;    
run;
/* My log*/
NOTE: Invalid data for StartDate in line 1 15-15.
 RULE:     ----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0                      
 1         Xing          2 19 2004 ACCT 28
 FirstName=Xing StartDate=. Department=ACCT _ERROR_=1 _N_=1
 NOTE: Invalid data for StartDate in line 2 15-15.
 2         Bob           5 22 2004 MKTG 28
 FirstName=Bob StartDate=. Department=MKTG _ERROR_=1 _N_=2
 NOTE: Invalid data for StartDate in line 3 15-15.
 3         Jorge         3 14 2004 EDUC 28
 FirstName=Jorge StartDate=. Department=EDUC _ERROR_=1 _N_=3
 NOTE: 3 records were read from the infile "/home/testuser/employee2.txt".
       The minimum record length was 28.
       The maximum record length was 28.

I have also attached the data file I am using.
Thanks once again.

ballardw
Super User

Three things.

First when you supply an informat and do not explicitly provide the columns the data is read from on the input statement then SAS reverts to list input.

Second the informat in your code is wrong. You are using DDMMYYYY but to read a value like " 2 19 2004" the data starts with MONTH (MM) not Day. MM can only have values of 01 to 12.

 

You have to use a method that forces reading all the characters with the informat and that is having the informat on the INPUT statement to force inclusion of the spaces.

data employee;
/*infile "/home/testuser/employee2.txt";*/
format startDate date9.;
input @1  FirstName $
      @15 StartDate mmddyy10. 
      @25 Department $
      ;    
datalines;
Xing          2 19 2004 ACCT
Bob           5 22 2004 MKTG
Jorge         3 14 2004 EDUC
;   

 

Sajid01
Meteorite | Level 14

Thanks for correcting my code and it worked. I used the following.

data employee;
infile "/home/theprogmin/employee2.txt";
Retain FirstName StartDate Department;
*informat StartDate mmddyy10.;
format startDate date9.;
input @1  FirstName $
      @15 StartDate mmddyy10.
      @25 Department $
      ;    
run;
      

Thanks once again.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 8 replies
  • 608 views
  • 4 likes
  • 4 in conversation