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 am trying to import a data set, but SAS is reading the table as if it was one variable, when there are 6. Heres a few lines of the data in the txt file:

 

Alabama Southeast 4 2 60 4
Arizona Southeast 4 3 81 5
Arkansas Southeast 3 1 52 1
California Southwest 5 6 150 9

 

which correspond to the variables STATE REGION VISITS SALESMEN SALES EXPENSES.

 

When I print the data set, proc contents says there is only one variable. The original data set has 47 observations. Here is the code I was trying to run:

libname Math338 'D:\SAS\Data';
data HW1;
data Math338.HW1; 
infile 'D:\SAS\Data\statedata.txt';
length state $ 35.0 region $ 10.0; 
input state $ region $ visits salesman sale expenses; 
label state = 'State Name'
region = 'Region Assignment'
visits = 'Visits from Regional Manager'
salesman = 'Numer of Salesmen'
sale = 'Gross Sales For Last Quarter'
expenses= 'Advertising Expenses For Last Quarter'
;
run;
*data Math338.HW1;
*set Math338.HW1;
*sale = sale * 10000;
*expenses = expenses * 1000;

*run;
proc contents data=Math338.HW1;
run;
proc print data= Math338.HW1;
run;

 

I am assuming that If I get SAS to realize there are 6 variables, then the commented out section will work too. I am also assuming sas is doing nothing with my label commands since it doesn't know there are more than one variable. Please help!

1 ACCEPTED SOLUTION

Accepted Solutions
valarievil
Obsidian | Level 7

Hello all. Thank you for your responses, but I have solved the problem.

 

IT WAS THE FILE LOCATION ugh. I do not have SAS installed on my computer I used 'Labspace' from my school's website (like Virtual box). SO instead of 'D:\SAS\Data' it needed to be '\\Client\D$\SAS\Data' and everything worked fine. 

View solution in original post

4 REPLIES 4
hashman
Ammonite | Level 13

@valarievil:

You need to construct the INPUT statement correctly, e.g.:

data Math338.HW1 ;                                                                                                                      
  infile "D:\SAS\Data\statedata.txt" ;                                                                                                  
  input state  :$35.                                                                                                                    
        region :$10.                                                                                                                    
        visits                                                                                                                          
        salesman                                                                                                                        
        sale                                                                                                                            
        expenses                                                                                                                        
  ;                                                                                                                                     
  label state    = 'State Name'                                                                                                         
        region   = 'Region Assignment'                                                                                                  
        visits   = 'Visits from Regional Manager'                                                                                       
        salesman = 'Numer of Salesmen'                                                                                                  
        sale     = 'Gross Sales For Last Quarter'                                                                                       
        expenses = 'Advertising Expenses For Last Quarter'                                                                              
  ;                                                                                                                                     
run ;                                                                                                                                   
                     

Another way is to set the informats first and then use the _ALL_ list:

data Math338.HW1 ;                                                                                                                      
  infile "D:\SAS\Data\statedata.txt" ;                                                                                                  
  informat state  $35.                                                                                                                  
           region $10.                                                                                                                  
           visits salesman sale expenses 8.                                                                                             
  ;                                                                                                                                     
  input (_all_) (:) ;                                                                                                                   
  label state    = 'State Name'                                                                                                         
        region   = 'Region Assignment'                                                                                                  
        visits   = 'Visits from Regional Manager'                                                                                       
        salesman = 'Numer of Salesmen'                                                                                                  
        sale     = 'Gross Sales For Last Quarter'                                                                                       
        expenses = 'Advertising Expenses For Last Quarter'                                                                              
  ;                                                                                                                                     
run ;                                  

Kind regards

Paul D. 

Tom
Super User Tom
Super User

That can't be the issue.

 

The original program was already defining the length of the character variables before using them in the INPUT statement.  So changing the code to force SAS to guess how long to make the variables based on the informat used in the INPUT statement in your first example or the informat attached to the variable in the INFORMAT statement in your second example would not change the behavior of the INPUT statement.

Tom
Super User Tom
Super User

Your program looks right, assuming that all the values are present on every line.  Please show the log and possible post example of the data lines.

 

Make sure the character you see between the values is an actual space and not something else, perhaps a tab.

 

Also if it is just the last value that is not being read right then perhaps the file has CR+LF as end of line markers and SAS is using on the LF as the end of line marker and so the extra CR at the end of each line will make the value invalid to be read as a number.  You can use the TERMSTR=CRLF option on the INFILE statement to tell SAS what end of line marker to look for.

 

If you have missing values and they are not marked with period as a place holder then SAS will hunt for more values on the next line. You can stop that by using the TRUNCOVER option on the INFILE statement.  But if the un-represented value is in the middle of the line the other values might get read into the wrong variables.  Again examples of the lines that cause trouble will help get a better answer.

valarievil
Obsidian | Level 7

Hello all. Thank you for your responses, but I have solved the problem.

 

IT WAS THE FILE LOCATION ugh. I do not have SAS installed on my computer I used 'Labspace' from my school's website (like Virtual box). SO instead of 'D:\SAS\Data' it needed to be '\\Client\D$\SAS\Data' and everything worked fine. 

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 929 views
  • 0 likes
  • 3 in conversation