DATA Step, Macro, Functions and more

Read file where data is scattered across multiple lines

Accepted Solution Solved
Reply
New Contributor
Posts: 3
Accepted Solution

Read file where data is scattered across multiple lines


I have data in the .txt file as:
VALUE   ADM_LN2G                                                           
               1     = 'ENGLISH'                                           
               2     = 'FRENCH'                                            
               3     = 'ARABIC'                                            
               4     = 'CHINESE'                                           
               5     = 'CREE'                                              
               6     = 'GERMAN'                                            
               7     = 'GREEK'                                             
                   ;                                                       
VALUE   ADM_LNG                                                            
              24     = 'INUKTITUT'                                         
              90     = 'OTHER'                                             
              96     = 'NOT APPLICABLE'                                    
              97     = 'DON''T KNOW'                                       
              98     = 'REFUSAL'                                           
              99     = 'NOT STATED'                                        
                   ;
.
.
.

 I want to separate out each value in different dataset.
Output expected:
Dataset 1 : ADM_LN2G
 VALUE ADM_LN2G
 1 = 'ENGLISH'
 2 = 'FRENCH'
 3 = 'ARABIC'
 4 = 'CHINESE'
 5 = 'CREE'
 6 = 'GERMAN'
 7 = 'GREEK'
 ;

 Dataset 2: ADM_LNG
VALUE   ADM_LNG                                                            
              24     = 'INUKTITUT'                                         
              90     = 'OTHER'                                             
              96     = 'NOT APPLICABLE'                                    
              97     = 'DON''T KNOW'                                       
              98     = 'REFUSAL'                                           
              99     = 'NOT STATED'
                   ;


etc..

For every VALUE there is ";" at the end.
Please help.
Thanks in advance!!
 


Accepted Solutions
Solution
‎04-25-2016 12:55 PM
Super User
Super User
Posts: 7,401

Re: Read file where data is scattered across multiple lines

That looks like a proc format text file your readin in, yes?  If so why not just create the format catalog with the code and create a dataset from that, so:

%include "<your_file>.sas";

/* The above should create a format catalog then */
proc format library=<library> cntlout=<library>.<dataset>;
run;

What this does is read the text file as a proc format, which will create a format catalog - in work lib if not supplied.  From that use proc format and cntlout to create a dataset of the values. 

View solution in original post


All Replies
Super User
Posts: 5,083

Re: Read file where data is scattered across multiple lines

There is a ";" as part of the text file.  Maybe that's where it's coming from.

 

If you would like someone to fix your program, you will have to actually show your program.

 

New Contributor
Posts: 3

Re: Read file where data is scattered across multiple lines

I wish I had the program but I have got no idea how to read this file in a way that will give me desired output. I know how to read the data line by line or delimited but not when data is delimited and is spread across multiple lines.
Super User
Posts: 17,831

Re: Read file where data is scattered across multiple lines

I think you're misunderstanding. You're not supposed to read that file, it's a program that creates the format. You need to run the program.

 

If there's no proc format at the top of the file add it and run the code. 

 

proc format;
VALUE   ADM_LN2G                                                           
               1     = 'ENGLISH'                                           
               2     = 'FRENCH'                                            
               3     = 'ARABIC'                                            
               4     = 'CHINESE'                                           
               5     = 'CREE'                                              
               6     = 'GERMAN'                                            
               7     = 'GREEK'                                             
                   ;                                                       
VALUE   ADM_LNG                                                            
              24     = 'INUKTITUT'                                         
              90     = 'OTHER'                                             
              96     = 'NOT APPLICABLE'                                    
              97     = 'DON''T KNOW'                                       
              98     = 'REFUSAL'                                           
              99     = 'NOT STATED'                                        
                   ;
run;
Solution
‎04-25-2016 12:55 PM
Super User
Super User
Posts: 7,401

Re: Read file where data is scattered across multiple lines

That looks like a proc format text file your readin in, yes?  If so why not just create the format catalog with the code and create a dataset from that, so:

%include "<your_file>.sas";

/* The above should create a format catalog then */
proc format library=<library> cntlout=<library>.<dataset>;
run;

What this does is read the text file as a proc format, which will create a format catalog - in work lib if not supplied.  From that use proc format and cntlout to create a dataset of the values. 

New Contributor
Posts: 3

Re: Read file where data is scattered across multiple lines

Thanks RW9. It worked.
☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 279 views
  • 2 likes
  • 4 in conversation