DATA Step, Macro, Functions and more

read a dataset within a dataset

Accepted Solution Solved
Reply
Contributor
Posts: 37
Accepted Solution

read a dataset within a dataset

Hi All,

 

Its pretty urgent!! So a quick help will be appreciated Smiley Happy

 

I have a dataset say : new.file.data

inside this dataset there is only one record which is itself a dataset : edi.batch.one

 

I want to read the inner dataset  ' edi.batch.one '  which is present inside  'new.file.data '

problem : the only physical file name I can provide is for 'new.file.data ' and sas doesnt read a file without it being assigned to a dd statement

 

Help please


Accepted Solutions
Solution
‎12-19-2016 08:16 PM
Respected Advisor
Posts: 4,173

Re: read a dataset within a dataset

[ Edited ]
Posted in reply to sayanapex06

O.K: So you've got two flat files (and not SAS libraries)

- flat file1 contains a single records which is the name of another flat file (flat file2)

- flat file2 contains the data you want to read via a SAS datastep. The name of the file can change, the structure is always the same

 

Is above correct?

 

If yes then we need a Filename and not a Libname statement.

 

Code along the line of below should do the job.

//input1 dd dsn=CHMICST.LISTCAT.OUTPUT9.NEW,disp=shr

  /* read single record from input1 containing another filename */
  data _null_;
    infile input1 lrcl=200;
    input;
    call symputx('targetFile',_infile_);
    stop;
  run;

  /* now read data from flatfile with data */
  filename wantfile "&targetFile" disp=shr;
  data want;
    infile wantfile lcrel=... truncover dlm=... dsd ...;

    /* length and attrib statement for input variables to be defined before input statement */
    length @1 ............ $x ;

    input @1 .......... $x  ;
      .............................. ;
    ................................. ;
    (.......) are for all the other fields/variables..
  ;
  run;

 

View solution in original post


All Replies
Super User
Posts: 19,837

Re: read a dataset within a dataset

Posted in reply to sayanapex06

I'm not sure I follow. If you post a sample it may help. 

 

You can use filename and other functions to create the references you need.  

http://support.sas.com/documentation/cdl/en/hostwin/69955/HTML/default/viewer.htm#n0ejqm9d28y6vcn1o7...

Super User
Posts: 5,515

Re: read a dataset within a dataset

Posted in reply to sayanapex06

From your description, it's not exactly clear which DD statement(s) you already have, and what your data set names are.  But there are other ways under MVS to locate a data set, other than coding a DD statement in the JCL.  You can have SAS define the libname within the SAS code.  Now you need to be able to include the location of the file.  If you don't know it, SAS won't know it either.

Contributor
Posts: 37

Re: read a dataset within a dataset

Posted in reply to Astounding

cool.. here's my problem elaborated....

my input dataset : CHMICST.LISTCAT.OUTPUT9.NEW

in jcl u have to code as : //input1 dd dsn=CHMICST.LISTCAT.OUTPUT9.NEW,disp=shr

and within this CHMICST.LISTCAT.OUTPUT9.NEW dataset I have a record, only one single record. And the record is :

EDIB88T.NTRCS1T.D121616.T191033.M708600.CTLR  

 

SO you see the record is itself a dataset,

 

Can you tell me how to read EDIB88T.NTRCS1T.D121616.T191033.M708600.CTLR   ?

Respected Advisor
Posts: 4,173

Re: read a dataset within a dataset

[ Edited ]
Posted in reply to sayanapex06

You don't need a JCL dd statement to assign a libname. You can also assign a libname or filename via SAS syntax.

 

In your case:

- Use a data _null_ step and create a macro variable using call symputx() which contains the value of the file you want to read.

- Use the macro variable in a library assignment.

 

data _null_;
  set input1.new;
  call symputx('targetFile',<var from new ds>);
  stop;
run;

libname wantlib "&targetFile" disp=shr;

data want;
  set wantlib.<your ds>;
  ...
run;

 

Contributor
Posts: 37

Re: read a dataset within a dataset

Thanks a lot !!

 

But could you please elaborate what is this var from new ds ( the edi file...)

 

and also this line : wantlib.<your ds>;

Contributor
Posts: 37

Re: read a dataset within a dataset

Posted in reply to sayanapex06

@Patrick the above

Respected Advisor
Posts: 4,173

Re: read a dataset within a dataset

[ Edited ]
Posted in reply to sayanapex06

It's for me really long ago that I've used SAS under z/OS so no more that "fluent" in reading JCL.

 

A SAS library contains SAS tables. Under z/OS such a library is stored in a file and then the file is formatted internally by SAS. So you need to assign a libname statement for SAS to read the tables within this file.

 

//input1 dd dsn=CHMICST.LISTCAT.OUTPUT9.NEW,disp=shr

You're allocating a file here. How do you read the record in it using SAS?

 

Does your target file contain text or SAS tables? If it's SAS tables then you need a libname statement to assign the library and then use a two level name <libref>.<table name> to get access to a specific table within this library. That's the <wantlib>.<your ds> syntax.

 

Are we dealing with a SAS library or is it just a flat file?

 

 

Contributor
Posts: 37

Re: read a dataset within a dataset

@Patrick  it is a flat file

in z/OS i.e ; by using jcl

we read a file assigned to a ddname like this :

 

data input;

infile input1;

input @1 .......... $x  ;

.............................. ;

length @1 ............ $x ;

................................. ;

(.......) are for all the other fields/variables..

 

SO you got the idea right?

 

Now my question comes here,

 

this input1 file which is our output9.new file has  a record ... and the record is itself a dataset.. How can you read that dataset when you are not able to assign it to a dd name ?? ( cause the record changes according to situation.. and so does the dataset(record) ) .. hope u finally got me

 

Solution
‎12-19-2016 08:16 PM
Respected Advisor
Posts: 4,173

Re: read a dataset within a dataset

[ Edited ]
Posted in reply to sayanapex06

O.K: So you've got two flat files (and not SAS libraries)

- flat file1 contains a single records which is the name of another flat file (flat file2)

- flat file2 contains the data you want to read via a SAS datastep. The name of the file can change, the structure is always the same

 

Is above correct?

 

If yes then we need a Filename and not a Libname statement.

 

Code along the line of below should do the job.

//input1 dd dsn=CHMICST.LISTCAT.OUTPUT9.NEW,disp=shr

  /* read single record from input1 containing another filename */
  data _null_;
    infile input1 lrcl=200;
    input;
    call symputx('targetFile',_infile_);
    stop;
  run;

  /* now read data from flatfile with data */
  filename wantfile "&targetFile" disp=shr;
  data want;
    infile wantfile lcrel=... truncover dlm=... dsd ...;

    /* length and attrib statement for input variables to be defined before input statement */
    length @1 ............ $x ;

    input @1 .......... $x  ;
      .............................. ;
    ................................. ;
    (.......) are for all the other fields/variables..
  ;
  run;

 

Contributor
Posts: 37

Re: read a dataset within a dataset

@Patrick  awesome man!! Thanks a ton for this quick and easy solution..... learnt this...

 

One more thing... if the flat file 2 which is my record, which changes situation wise...

what if I have got two record lines i.e; two datsets to read.....

 

can we loop it?? or the same would work ??

Respected Advisor
Posts: 4,173

Re: read a dataset within a dataset

[ Edited ]
Posted in reply to sayanapex06

what if I have got two record lines i.e; two datsets to read.....

I wanted to keep the code simple for you but there is also a solution for multiple files. Example 5 under the following link shows you how that's done. 

http://support.sas.com/documentation/cdl/en/lestmtsref/69738/HTML/default/viewer.htm#n1rill4udj0tfun...

 

For  your use case the code should be something close to below.

//input1 dd dsn=CHMICST.LISTCAT.OUTPUT9.NEW,disp=shr

    data want;
     length fileloc myinfile $ 300;
     infile input1 truncover;
     input fileloc $ ; /* read instream data       */
    /* The INFILE statement closes the current file 
       and opens a new one if FILELOC changes value 
       when INFILE executes                        */
     infile dummy filevar=fileloc 
            filename=myinfile end=done
            lcrel=... truncover dlm=... dsd ...
            ; 
    /* DONE set to 1 when last input record read  */
     do while(not done);
    /* Read all input records from the currently  */
    /* opened input file, write to ALLSALES       */

      length @1 ............ $x ;

      input @1 .......... $x  ;
        .............................. ;
      ................................. ;
      (.......) are for all the other fields/variables..
       output;
     end;
     put 'Finished reading ' myinfile=; 
  run;

 

P.S: Don't use the term "dataset" for flat files as that's confusing for SAS guys like me. In a SAS context SAS tables are often referred to as datasets - that's why I thought in the beginning you're dealing with SAS libraries.

Contributor
Posts: 37

Re: read a dataset within a dataset

Thanks @Patrick  .. could you please explain...

Respected Advisor
Posts: 4,173

Re: read a dataset within a dataset

Posted in reply to sayanapex06

@sayanapex06

Not sure what you want me to explain. Have a look at the link I've posted and especially at example 5. There are explanations there in the docu of how things work and the code I've posted is very close to example 5.

Contributor
Posts: 37

Re: read a dataset within a dataset

@Patrick Its not working as expected.. I am getting errors.. Cant we place the previous example you gave and place it inside a loop?

☑ This topic is solved.

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

Discussion stats
  • 20 replies
  • 396 views
  • 3 likes
  • 4 in conversation