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

Hi All,

 

Its pretty urgent!! So a quick help will be appreciated 🙂

 

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

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

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

20 REPLIES 20
Reeza
Super User

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...

Astounding
PROC Star

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.

sayanapex06
Obsidian | Level 7

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   ?

Patrick
Opal | Level 21

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;

 

sayanapex06
Obsidian | Level 7

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>;

sayanapex06
Obsidian | Level 7

@Patrick the above

Patrick
Opal | Level 21

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?

 

 

sayanapex06
Obsidian | Level 7

@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

 

Patrick
Opal | Level 21

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;

 

sayanapex06
Obsidian | Level 7

@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 ??

Patrick
Opal | Level 21

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.

sayanapex06
Obsidian | Level 7

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

Patrick
Opal | Level 21

@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.

sayanapex06
Obsidian | Level 7

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

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 20 replies
  • 2162 views
  • 3 likes
  • 4 in conversation