IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

Basic Input statement - SAS


IBM Mainframe Forums -> All Other Mainframe Topics
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
vasanthz

Global Moderator


Joined: 28 Aug 2007
Posts: 1742
Location: Tirupur, India

PostPosted: Mon Jul 16, 2012 8:32 pm
Reply with quote

Hello,
There is a SAS program,
Code:
DATA WELLS;           
INFILE CARDS DLM=', ';
INPUT (VAR1-VAR4) ($);
CARDS;               
A1,                   
B2,C3,               
D4                   
E1,F2,               
G3,H4                 
A1,B2,C3,             
D4                   
;                     
RUN;                 

This program quietly reads data and produces a nice output like below,
Code:
VAR1    VAR2    VAR3    VAR4
                           
 A1      B2      C3      D4
 E1      F2      G3      H4
 A1      B2      C3      D4


But if the data has embedded spaces in them then comes the problem.
Could someone please let me know how to read the below data into 4 variables.
Code:
A 1,     
B 2,C3,   
D4       
E 1,F2,   
G3,H 4   
A1,B 2,C3,
D 4       

Expected output:
Code:

VAR1    VAR2    VAR3    VAR4
                           
 A 1     B 2     C3      D4
 E 1     F2      G3      H 4
 A1      B 2     C3      D 4


I could try @@ but that maybe overkill.

Thanks in advance,
Back to top
View user's profile Send private message
expat

Global Moderator


Joined: 14 Mar 2007
Posts: 8797
Location: Welsh Wales

PostPosted: Mon Jul 16, 2012 9:10 pm
Reply with quote

Have you tried INFILE CARDS DLM=', ' DSD;
Back to top
View user's profile Send private message
vasanthz

Global Moderator


Joined: 28 Aug 2007
Posts: 1742
Location: Tirupur, India

PostPosted: Mon Jul 16, 2012 10:54 pm
Reply with quote

Hi Expat,
Tried DSD but did'nt work. Some values are not read into variables correctly.
Back to top
View user's profile Send private message
expat

Global Moderator


Joined: 14 Mar 2007
Posts: 8797
Location: Welsh Wales

PostPosted: Mon Jul 16, 2012 11:01 pm
Reply with quote

Well really Vasanthz, you have not posted the actual output to compare against the expected output, now go stand in the naughty corner icon_lol.gif
Back to top
View user's profile Send private message
dbzTHEdinosauer

Global Moderator


Joined: 20 Oct 2006
Posts: 6966
Location: porcelain throne

PostPosted: Mon Jul 16, 2012 11:23 pm
Reply with quote

EXPAT wrote:
you have not posted the actual output to compare against the expected output,

you mean to allow for intuitive analysis of what is going on?
EXPAT, you expect to much.
Vsanathz, you are a much better technician than your post implies.
Back to top
View user's profile Send private message
Robert Sample

Global Moderator


Joined: 06 Jun 2008
Posts: 8696
Location: Dubuque, Iowa, USA

PostPosted: Mon Jul 16, 2012 11:26 pm
Reply with quote

Vasanthz, the & (ampersand) format modifier is a possibility. However, as this page indicates the requirement is for variables to be separated by at least two spaces: Reading embedded spaces with SAS
Back to top
View user's profile Send private message
expat

Global Moderator


Joined: 14 Mar 2007
Posts: 8797
Location: Welsh Wales

PostPosted: Tue Jul 17, 2012 12:11 am
Reply with quote

dbzTHEdinosauer wrote:

Vsanathz, you are a much better technician than your post implies.

Good spot Dick, my comments made were somewhat tongue in cheek
Back to top
View user's profile Send private message
vasanthz

Global Moderator


Joined: 28 Aug 2007
Posts: 1742
Location: Tirupur, India

PostPosted: Tue Jul 17, 2012 12:14 am
Reply with quote

Hi,
Thanks all in trying to help :-)
My bad. I forgot to copy paste the output which I got when I ran the program with 2nd set of data(possible culprit - hangover or lucid dreaming).
I will post it tomorrow when im at work.

Robert, & is a nice idea, I will try it out to see if I am able to get it working.

Regards,
Back to top
View user's profile Send private message
vasanthz

Global Moderator


Joined: 28 Aug 2007
Posts: 1742
Location: Tirupur, India

PostPosted: Tue Jul 17, 2012 12:41 am
Reply with quote

Hello,
I powered my PC SAS and tested Robert's suggestion. But SAS seems to ignore the comma as delimited when I tried "&".

Here are some of the things I tried out,
Code:

Expected output:
VAR1    VAR2    VAR3    VAR4
                           
 A 1     B 2     C3      D4
 E 1     F2      G3      H 4
 A1      B 2     C3      D 4

For the statement,
INFILE CARDS DLM=', ';
output was,
                                 Obs    VAR1    VAR2    VAR3    VAR4

                                  1      A       1       B       2
                                  2      D4      E       1       F2
                                  3      G3      H       4       A1

For
INFILE CARDS DLM=', ' DSD;
output:
                                 Obs    VAR1    VAR2    VAR3    VAR4

                                  1      A       1
                                  2      B       2       C3
                                  3      D4
                                  4      E       1       F2
                                  5      G3      H       4
                                  6      A1      B       2       C3
                                  7      D       4

For
INFILE CARDS DLM=',' ;
INPUT (VAR1-VAR4) (& $) ;
Output:
                              Obs    VAR1     VAR2      VAR3      VAR4

                               1     A 1               B 2,C3
                               2     D4      E 1,F2              G3,H 4

For
INFILE CARDS DLM=', ' ;
INPUT (VAR1-VAR4) (& $) ;
Output:
                               Obs    VAR1     VAR2     VAR3     VAR4

                                1     A 1     B 2,C3     D4     E 1,F2


Quote:
Vsanathz, you are a much better technician than your post implies.
Thanks for the compliment/pun (:
Back to top
View user's profile Send private message
dbzTHEdinosauer

Global Moderator


Joined: 20 Oct 2006
Posts: 6966
Location: porcelain throne

PostPosted: Tue Jul 17, 2012 2:16 am
Reply with quote

was not a pun, it is a statement of fact.

you know that one can not even start to 'debug' until
one knows what is happening.
Back to top
View user's profile Send private message
PeterHolland

Global Moderator


Joined: 27 Oct 2009
Posts: 2481
Location: Netherlands, Amstelveen

PostPosted: Tue Jul 17, 2012 12:12 pm
Reply with quote

Try this :
Code:

INFILE CARDS DLM=',';
INPUT (VAR1-VAR4) ($8.);
Back to top
View user's profile Send private message
vasanthz

Global Moderator


Joined: 28 Aug 2007
Posts: 1742
Location: Tirupur, India

PostPosted: Tue Jul 17, 2012 1:49 pm
Reply with quote

Hello Peter, I tried out your suggestion.
Code:
INFILE CARDS DLM=',';   
INPUT (VAR1-VAR4) (:$8.);

here is the output,
Code:

VAR1    VAR2    VAR3    VAR4
                           
 A1              B2      C3
 D4      E1      F2         
 G3      H4      A1      B2


The problem I think is, for the record
A 1,
SAS reads A1 into VAR1 and when it encounters "," it reads the spaces into VAR2.
I could supress this behavior by mentioning spaces in DLM like
DLM='<space>,'. But mentioning space in DLM option causes incorrect reading of data with embedded spaces in them.

To test if this is the case,
I ran the below program with FB files and VB files.
INFILE 'WELLS.EXTERNAL.FILE.' DLM=',';
INPUT (VAR1-VAR4) ($);

For VB external file the output was same as expected output,
Code:
VAR1    VAR2    VAR3    VAR4
                           
A 1     B 2      C3     D4 
E 1     F2       G3     H 4
A1      B 2      C3     D 4


For FB external file the output was not correct due to trailing blanks.
Code:
VAR1    VAR2    VAR3    VAR4
                           
A 1             B 2     C3 
D4      E 1     F2         
G3      H 4     A1      B 2

So I guess I will copy the whole file into a VB file and then process it.
If there are any other alternative please let me know.

Thank you,
Back to top
View user's profile Send private message
PeterHolland

Global Moderator


Joined: 27 Oct 2009
Posts: 2481
Location: Netherlands, Amstelveen

PostPosted: Tue Jul 17, 2012 2:01 pm
Reply with quote

(:$8.); was not in my sample.
Back to top
View user's profile Send private message
vasanthz

Global Moderator


Joined: 28 Aug 2007
Posts: 1742
Location: Tirupur, India

PostPosted: Tue Jul 17, 2012 2:38 pm
Reply with quote

Peter, Thank you in trying to help.
I assumed you meant (:$8.) instead of ($8.) since this was list input.

Because if we read a delimited file using list input without a format modifier ($8.) then input statement will ignore the delimiter specified in DLM option and read 8 bytes of data even if it encounters a delimiter.

without format modifier:
Code:
DATA wells;             
INFILE CARDS DLM=',';   
INPUT VAR1 $8. VAR2 $8.;
CARDS;                   
1,3                     
AB,4                     
;                       
RUN;                     

output:
Code:
Obs    VAR1    VAR2
                   
 1     1,3         
 2     AB,4       

With format modifier:
Code:
DATA wells;               
INFILE CARDS DLM=',';     
INPUT VAR1 :$8. VAR2 :$8.;
CARDS;                   
1,3                       
AB,4                     
;                         
RUN;                     

Output:
Code:

Obs    VAR1    VAR2
                   
 1      1       3 
 2      AB      4 
Back to top
View user's profile Send private message
PeterHolland

Global Moderator


Joined: 27 Oct 2009
Posts: 2481
Location: Netherlands, Amstelveen

PostPosted: Tue Jul 17, 2012 2:41 pm
Reply with quote

Yes, you are totally right.
Back to top
View user's profile Send private message
expat

Global Moderator


Joined: 14 Mar 2007
Posts: 8797
Location: Welsh Wales

PostPosted: Tue Jul 17, 2012 4:00 pm
Reply with quote

Haven't got access to try this but maybe you could icon_biggrin.gif

It's a variation of code that I have used for processing a .csv file, and all I have done is remove the TRUNCOVER from the infile as you are not using a formatted .csv per se as your input, so without either of the MISSOVER or TRUNCOVER options specified should fill the obs as you want it.

Code:
  INFILE FILE1 DLM="," ;
  INPUT N1:$8.
        N2:$8.
        N3:$8.
        N4:$8. ;
RUN;
Back to top
View user's profile Send private message
vasanthz

Global Moderator


Joined: 28 Aug 2007
Posts: 1742
Location: Tirupur, India

PostPosted: Wed Jul 18, 2012 9:54 am
Reply with quote

Hello Expat,
Still no joy with the code, here is the output of the above code,
Code:
N1          N2          N3     N4
                                 
A 1                     B 2    C3
D4          E 1         F2       
G3          H 4         A1     B 2


Input:
Code:

A 1,     
B 2,C3,   
D4       
E 1,F2,   
G3,H 4   
A1,B 2,C3,
D 4       

Regards,
Back to top
View user's profile Send private message
Bill Woodger

Moderator Emeritus


Joined: 09 Mar 2011
Posts: 7309
Location: Inside the Matrix

PostPosted: Wed Jul 18, 2012 12:49 pm
Reply with quote

Maybe it is a "philosophical" thing. Where you have a trailing comma, the implication (being made by SAS) is that you have another field containing a blank.

In fact, if you did have a final field that could contain a blank, that is how you'd want SAS to behave.

Whether the behaviour can be modified, I have no idea.
Back to top
View user's profile Send private message
vasanthz

Global Moderator


Joined: 28 Aug 2007
Posts: 1742
Location: Tirupur, India

PostPosted: Wed Jul 18, 2012 2:09 pm
Reply with quote

Yes Bill, It seems to be the way SAS was designed. As a temporary workaround, I have copied the data to VB file and processed it until alternative way is found.
Quote:
Whether the behaviour can be modified, I have no idea.

Me too
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> All Other Mainframe Topics

 


Similar Topics
Topic Forum Replies
No new posts TRIM everything from input, output co... DFSORT/ICETOOL 1
No new posts Extracting Variable decimal numbers f... DFSORT/ICETOOL 17
No new posts force tablespace using LISTDEF input DB2 1
No new posts Two input files & writing counter... DFSORT/ICETOOL 12
No new posts Use input file with OMIT rcd keys? DFSORT/ICETOOL 15
Search our Forums:

Back to Top