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

Hi All ,

 

I am quite new to SAS . Just started learning it . Please suggest what function best suits here .


Input File :
Total Exceptions Were Found         
Exceptions Were ignored              
Total Violations Were Found
============================================
Object     : PLATFORM              
Type       : SOFTWARE                       
Attribute  : LISENCE                      
Should Be  : INTACT                       
Actual     : INTACT                             
Standard   : NO                            
===========================================
===========================================
Object     : MACHINE                   
Type       : HARDWARE                      
Attribute  : CAPACITY                      
Should Be  : AMPLE          
Actual     : UNDER      
Baseline   : YES

For the above Input file , how can I print it in Colum wise

Example :

Object Type Attribute Should Be Actual Standard   
PLATFORM SOFTWARE LISENCE INTACT INTACT NO
MACHINE HARDWARE CAPACITY AMPLE UNDER YES    

I tried to use the deliminartor but its not giving me the desired output .
Please suggest what function to use .     

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

@sams54156 wrote:

Hi All ,

 

I am quite new to SAS . Just started learning it . Please suggest what function best suits here .


Input File :
Total Exceptions Were Found         
Exceptions Were ignored              
Total Violations Were Found
============================================
Object     : PLATFORM              
Type       : SOFTWARE                       
Attribute  : LISENCE                      
Should Be  : INTACT                       
Actual     : INTACT                             
Standard   : NO                            
===========================================
===========================================
Object     : MACHINE                   
Type       : HARDWARE                      
Attribute  : CAPACITY                      
Should Be  : AMPLE          
Actual     : UNDER      
Baseline   : YES

For the above Input file , how can I print it in Colum wise

Example :

Object Type Attribute Should Be Actual Standard   
PLATFORM SOFTWARE LISENCE INTACT INTACT NO
MACHINE HARDWARE CAPACITY AMPLE UNDER YES    

I tried to use the deliminartor but its not giving me the desired output .
Please suggest what function to use .     


Your main issue is that the format you show for your file is a moderately (or worse depending on how regular the content is) complex file.

The basic SAS input statement for a delimited file expects all variables to be on a single row in the data. When you have values on multiple lines in a data set then you have to provide instructions on which lines to read which values.

Further complicating things is that each block of content (I am assuming as you did not say) appearing between the lines of

============================================

have different actual content. First you appear to have a header section that starts with the "Total Exceptions". You don't indicate if that should be kept.

Second you would want to read data only from lines after the divider.

Third is varying content.

The first block has Standard   : NO           but the second block has Baseline   : YES . So "standard" does not seem to actually appear in the second block.

And also it appears that the number of divider lines may be different inside the file.

If every "record" has exactly 6 lines and you want the 6th line always be in a variable named Standard that is possible. IF not then a more complete description of the file and the desired result is going to be needed.

It is a good idea to post example input text in a code box opened with the forum {I} icon because the message windows here will "clean up" spaces and so the text you paste may not actually be what your file looks like.

 

This may get you started:

data work.raw;
   infile datalines truncover;
   length Object Type Attribute Should_Be Actual Standard  dummy $ 15.;
   input @;
   if _infile_ =: "==============" then do;
      /* go to next line*/
      input;
      input @':' object
           /@':' type
           /@':' Attribute
           /@':' Should_Be
           /@':' Actual
           /@':' Standard
           / dummy
      ;
      output;
   end;
   else input;
   drop dummy;
datalines;
Total Exceptions Were Found         
Exceptions Were ignored              
Total Violations Were Found
============================================
Object     : PLATFORM              
Type       : SOFTWARE                       
Attribute  : LISENCE                      
Should Be  : INTACT                       
Actual     : INTACT                             
Standard   : NO                            
===========================================
===========================================
Object     : MACHINE                   
Type       : HARDWARE                      
Attribute  : CAPACITY                      
Should Be  : AMPLE          
Actual     : UNDER      
Baseline   : YES
===========================================
;
run;

The datalines is to simulate you input file. You would not include that. Instead the INFILE would point to your file.

 

The length statement sets the variables as character and provides an expected length for them. If you have a better idea of the actual content for each variable you can customize the length. Note that your variable "Should be" is not a valid SAS variable name so I place and _ instead of the space. Dummy is a variable to read the following =========== divider line.

@the first input @; brings the current line of the file into a buffer to examine with the SAS special variable _infile_.

If it starts with a number of ===== characters (the =: compares the start of a string) then that is a divider line.

@AT which point we use one input; to advance to the next line that should have data. The @':' instruction on the input line says to advance the read pointer in the file to that position to start reading data. The / say "go to the next line". The / dummy says to read the following divider line.

 

Some caveats: If there are not exactly the same number of rows of data in the same order each time this is not going to work.

This expects two rows of ========== between the blocks of data and that the last record has a =========== following it.

If the value of any of these variables has a space in it then only the first word will be read.

 

Proc print would be the way to display the results if that is what you meant by print them column wise.

View solution in original post

2 REPLIES 2
Reeza
Super User
INPUT statement is what you need.
See the documentation for several examples on how to read multi-line records into a single observation.

https://documentation.sas.com/?docsetId=basess&docsetTarget=p0s16wvzu0z9q7n0zmxia30s6qyc.htm&docsetV...
ballardw
Super User

@sams54156 wrote:

Hi All ,

 

I am quite new to SAS . Just started learning it . Please suggest what function best suits here .


Input File :
Total Exceptions Were Found         
Exceptions Were ignored              
Total Violations Were Found
============================================
Object     : PLATFORM              
Type       : SOFTWARE                       
Attribute  : LISENCE                      
Should Be  : INTACT                       
Actual     : INTACT                             
Standard   : NO                            
===========================================
===========================================
Object     : MACHINE                   
Type       : HARDWARE                      
Attribute  : CAPACITY                      
Should Be  : AMPLE          
Actual     : UNDER      
Baseline   : YES

For the above Input file , how can I print it in Colum wise

Example :

Object Type Attribute Should Be Actual Standard   
PLATFORM SOFTWARE LISENCE INTACT INTACT NO
MACHINE HARDWARE CAPACITY AMPLE UNDER YES    

I tried to use the deliminartor but its not giving me the desired output .
Please suggest what function to use .     


Your main issue is that the format you show for your file is a moderately (or worse depending on how regular the content is) complex file.

The basic SAS input statement for a delimited file expects all variables to be on a single row in the data. When you have values on multiple lines in a data set then you have to provide instructions on which lines to read which values.

Further complicating things is that each block of content (I am assuming as you did not say) appearing between the lines of

============================================

have different actual content. First you appear to have a header section that starts with the "Total Exceptions". You don't indicate if that should be kept.

Second you would want to read data only from lines after the divider.

Third is varying content.

The first block has Standard   : NO           but the second block has Baseline   : YES . So "standard" does not seem to actually appear in the second block.

And also it appears that the number of divider lines may be different inside the file.

If every "record" has exactly 6 lines and you want the 6th line always be in a variable named Standard that is possible. IF not then a more complete description of the file and the desired result is going to be needed.

It is a good idea to post example input text in a code box opened with the forum {I} icon because the message windows here will "clean up" spaces and so the text you paste may not actually be what your file looks like.

 

This may get you started:

data work.raw;
   infile datalines truncover;
   length Object Type Attribute Should_Be Actual Standard  dummy $ 15.;
   input @;
   if _infile_ =: "==============" then do;
      /* go to next line*/
      input;
      input @':' object
           /@':' type
           /@':' Attribute
           /@':' Should_Be
           /@':' Actual
           /@':' Standard
           / dummy
      ;
      output;
   end;
   else input;
   drop dummy;
datalines;
Total Exceptions Were Found         
Exceptions Were ignored              
Total Violations Were Found
============================================
Object     : PLATFORM              
Type       : SOFTWARE                       
Attribute  : LISENCE                      
Should Be  : INTACT                       
Actual     : INTACT                             
Standard   : NO                            
===========================================
===========================================
Object     : MACHINE                   
Type       : HARDWARE                      
Attribute  : CAPACITY                      
Should Be  : AMPLE          
Actual     : UNDER      
Baseline   : YES
===========================================
;
run;

The datalines is to simulate you input file. You would not include that. Instead the INFILE would point to your file.

 

The length statement sets the variables as character and provides an expected length for them. If you have a better idea of the actual content for each variable you can customize the length. Note that your variable "Should be" is not a valid SAS variable name so I place and _ instead of the space. Dummy is a variable to read the following =========== divider line.

@the first input @; brings the current line of the file into a buffer to examine with the SAS special variable _infile_.

If it starts with a number of ===== characters (the =: compares the start of a string) then that is a divider line.

@AT which point we use one input; to advance to the next line that should have data. The @':' instruction on the input line says to advance the read pointer in the file to that position to start reading data. The / say "go to the next line". The / dummy says to read the following divider line.

 

Some caveats: If there are not exactly the same number of rows of data in the same order each time this is not going to work.

This expects two rows of ========== between the blocks of data and that the last record has a =========== following it.

If the value of any of these variables has a space in it then only the first word will be read.

 

Proc print would be the way to display the results if that is what you meant by print them column wise.

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
  • 536 views
  • 0 likes
  • 3 in conversation