BookmarkSubscribeRSS Feed
Babloo
Rhodochrosite | Level 12

I would request someone to give suggestion for my questions.

1. I've a file called output_file.txt in unix and it has list of file names (e.g.) SASApp_STPServer_tmp_12345.log. Now I need to create a dataset by consolidating all the files read from output_file.txt.

Since files are coming from different systems (will have different folder structures) I can't use wildcards in infile statement like infile "/usr/sas/sas_config/Lev1/SASApp/Logs/SASApp_STPServer_tmp_*.log" dsd truncover filename=fname; However file layouts are same like below.

data log_analysis;

length fname filename $200;

infile "/usr/sas/sas_config/Lev1/SASApp/Logs/SASApp_STPServer_tmp_*.log" dsd truncover filename=fname;

input var : $ 3000.;

filename=fname;

var1 = _infile_;

if var1 = :'201';

Date_TimeStamp= scan(var1,1," ");

Status = scan(var1,2," ");

Processid = scan(var1,3," ");

userid = scan(var1,4," ");

Details = scan(var1,-1,'-');

drop var var1;

run;

This code will work only if file is from ' /usr/sas/sas_config/Lev1/SASApp/StoredProcessServer/Logs/'. Now I would like to know, how should I tweak this code, if I want to read /usr/sas/tir/test/loganalysis/output_file.txt.

Assume output_file.txt has filenmaes like SASApp_STPServer_tmp_12345.log JavaApp_STPServer_tmp_34745.log  SharepointApp_STPServer_tmp_72345.log

2. How to conditionally export the file?  e.g. I need to export .csv only if dataset has greater than 0 observations.

Thanks in advance for your help.

6 REPLIES 6
Babloo
Rhodochrosite | Level 12

I've the following question as well. In below why one data step works and the other not.

DATA _null_;
     SET InputSet NOBS=nobs;

     IF nobs = 0 then PUT "ERROR:  InputSet Table is empty";      /* Doesn't work. */
RUN;

DATA _null_;
     IF nobs = 0 then PUT "ERROR:  InputSet Table is empty";      /* Works. */
     SET InputSet NOBS=nobs;
RUN;

Kurt_Bremser
Super User

When the SET statement encounters an end-of-file condition, it immediately stops processing of the data step, including all statements that appear after the SET statement in the data step.

So your IF in the first example is never executed in case no obs are present.

In the second example, the IF is executed before the SET can stop the datastep.

The constant (that's what it is, basically) nobs is set before the data step starts iterating.

Another way is to use an IF 0 THEN SET statement. During compilation time nobs is assigned a value but the SET statement never executes. This will save processing time especially if there are many observations in your data set. Since the SET statement will never execute order of the statements doesn't matter.

 

DATA _null_;

if 0 then set inputset nobs=nobs;

IF nobs = 0 then PUT "ERROR:  InputSet Table is empty";   

RUN;

 

 

You can use the filevar= option on the infile statement to read in multiple files into the same SAS data set, as illustrated in these Samples:

 

http://support.sas.com/kb/24712

http://support.sas.com/kb/24710

 

Hope that helps!

Babloo
Rhodochrosite | Level 12

I've the code like below.

 

data dirlist;
  infile cards truncover;
  input file_names $300.;
  cards;
'/usr/sas/sas_config/Lev1/SASApp/StoredProcessServer/Logs/SASApp_STPServer_2015-08-30_tmptcmlva2_19142.log'
'/usr/sas/sas_config/Lev1/SASApp/StoredProcessServer/Logs/SASApp_STPServer_2015-08-29_tmptcmlva2_19142.log'
'/usr/sas/sas_config/Lev1/SASApp/StoredProcessServer/Logs/SASApp_STPServer_2015-08-28_tmptcmlva2_19142.log'
;
run;

Now I need to read all the files and append it to a single file as mentioned in data step. So I wrote a code as below. However, I could  not succeeed. See the log below.

 

There are records in my input files,but my code could not read it for some reasons. Please guide me.

 

DATA output_data_set;
set dirlist;
/* read the file references in variable called file_names */
INFILE  IN dsd truncover FILEVAR = file_names END = end_of_file LRECL=32000;
     DO WHILE (end_of_file = 0);
        input var : $ 3000.;
/*filename=file_names;*/
var1 = _infile_;
if var1 = :'201';
Date_TimeStamp= scan(var1,1," ");
Status = scan(var1,2," ");
Processid = scan(var1,3," ");
userid = scan(var1,4," ");
Details = scan(var1,-1,'-');
drop var var1;
        OUTPUT;
     END;
RUN;

 Log:

 

4          data dirlist;
5            infile cards truncover;
6            input file_names $300.;
7            cards;

NOTE: The data set WORK.DIRLIST has 3 observations and 1 variables.
NOTE: DATA statement used (Total process time):
      real time           0.04 seconds
      cpu time            0.00 seconds
      

7        !         
11         ;
12         run;
2                                                          The SAS System                           08:36 Friday, September 11, 2015

13         
14         DATA output_data_set;
15         set dirlist;
16         /* read the file references in variable called file_names */
17         INFILE  IN dsd truncover FILEVAR = file_names END = end_of_file LRECL=32000;
18              DO WHILE (end_of_file = 0);
19                 input var : $ 3000.;
20         /*filename=file_names;*/
21         var1 = _infile_;
22         if var1 = :'201';
23         Date_TimeStamp= scan(var1,1," ");
24         Status = scan(var1,2," ");
25         Processid = scan(var1,3," ");
26         userid = scan(var1,4," ");
27         Details = scan(var1,-1,'-');
28         drop var var1;
29                 OUTPUT;
30              END;
31         RUN;

NOTE: The variable file_names exists on an input data set, but was also specified in an I/O statement option.  The variable will 
      not be included on any output data set.
NOTE: The infile IN is:
      Filename=/usr/sas/sas_config/Lev1/SASApp/StoredProcessServer/Logs/SASApp_STPServer_2015-08-30_tmptcmlva2_19142.log,
      
      File List=('/usr/sas/sas_config/Lev1/SASApp/StoredProcessServer/Logs/SASApp_STPServer_2015-08-30_tmptcmlva2_19142.log' 
      '/apps/tir/test/loganalysis/
'),
      Owner Name=sassrv,Group Name=sas,
      Access Permission=rwxrwxr-x,
      Last Modified=Mon Aug 31 00:01:56 2015,
      File Size (bytes)=720559

NOTE: The infile IN is:
      Filename=/usr/sas/sas_config/Lev1/SASApp/StoredProcessServer/Logs/SASApp_STPServer_2015-08-29_tmptcmlva2_19142.log,
      
      File List=('/usr/sas/sas_config/Lev1/SASApp/StoredProcessServer/Logs/SASApp_STPServer_2015-08-29_tmptcmlva2_19142.log' 
      '/apps/tir/test/loganalysis/
'),
      Owner Name=sassrv,Group Name=sas,
      Access Permission=rwxrwxr-x,
      Last Modified=Sun Aug 30 00:01:46 2015,
      File Size (bytes)=720559

NOTE: The infile IN is:
      Filename=/usr/sas/sas_config/Lev1/SASApp/StoredProcessServer/Logs/SASApp_STPServer_2015-08-28_tmptcmlva2_19142.log,
      
      File List=('/usr/sas/sas_config/Lev1/SASApp/StoredProcessServer/Logs/SASApp_STPServer_2015-08-28_tmptcmlva2_19142.log' 
      '/apps/tir/test/loganalysis/
'),
      Owner Name=sassrv,Group Name=sas,
      Access Permission=rwxrwxr-x,
      Last Modified=Sat Aug 29 00:01:36 2015,
      File Size (bytes)=2815408

NOTE: 1 record was read from the infile IN.
      The minimum record length was 572.
      The maximum record length was 572.
NOTE: 1 record was read from the infile IN.
      The minimum record length was 572.
      The maximum record length was 572.
3                                                          The SAS System                           08:36 Friday, September 11, 2015

NOTE: 1 record was read from the infile IN.
      The minimum record length was 572.
      The maximum record length was 572.
NOTE: There were 3 observations read from the data set WORK.DIRLIST.
NOTE: The data set WORK.OUTPUT_DATA_SET has 0 observations and 5 variables.
NOTE: DATA statement used (Total process time):
      real time           0.07 seconds
      cpu time            0.00 seconds
      

 

RamKumar
Fluorite | Level 6

I'm looking for some guidance to complete this task. Thanks in advance.

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
  • 6 replies
  • 1962 views
  • 1 like
  • 4 in conversation