Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Where to place counts in SAS code?

Posted on 2013-02-06
8
Medium Priority
?
626 Views
Last Modified: 2013-11-16
I am trying to write counts to mylog.log file for file allrecords.dat (read from counts) and files file1.dat and file2.dat (written to counts).  
Where do I place writtencnt1+1 and writtencnt2+1 counts in code for file1.dat and file2.dat?

Note: readcnt+1 count is already placed for allrecords.dat file and it is working fine.  

DATA _NULL_;
 drop readcnt writtencnt1 writtencnt2; 
 infile "allrecords" end=lastrec;
   length readcnt  8 writtencnt1 8 writtencnt2 8;
   retain readcnt  0 writtencnt1 0 writtencnt2 0;
  input @1 Var1 $char20.;

  IF SUBSTR(Var1,1,1) = '1'  then File "h:\xxx\test1.txt";
   else IF SUBSTR(Var1,1,1) = '2' THEN File "h:\xxx\test2.txt";

  readcnt+1;
   PUT _INFILE_;

if lastrec then do;
          file mylog.log; 
         
          /***************************
          * Records read from allrecords.dat
          ***************************/
          put "Total records read " readcnt;
         
          /********************************
          * Records written to test1.dat
          ********************************/
          put "Total records written " writtencnt1;
         
          /********************************
          * Records written to test2.dat
          ********************************/
          put "Total records written " writtencnt2;
         
         end;
 run;
RUN;

Open in new window


Any examples, comments or suggestions will be greatly appreciated!
0
Comment
Question by:labradorchik
  • 4
  • 3
8 Comments
 
LVL 14

Expert Comment

by:Aloysius Low
ID: 38860294
first, try declaring your FILE statement before the DATA step.

if it is not required to achieve all in one step, then i would say step 1 is to read the data into 2 datasets, splitting them as you read. keep track of the records read and store into a macro variable

then step 2 is to output the contents of the 2 datasets produced into the 2 required .dat files, and store the numbers into a marco variable each

then step 3 is to print the log file with the 3 required numbers using values retrieved from the macro variables
0
 

Author Comment

by:labradorchik
ID: 38860337
Thank you lowaloysius for your comments and suggestions!
I already defined FILE statement before the DATA step, so that part is working fine.

Is there a way to initialize and outputting all these three counts in one DATA step?
0
 

Author Comment

by:labradorchik
ID: 38861013
I am really trying to initiate and output all three counts in one DATA step, is it possible?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 14

Accepted Solution

by:
Aloysius Low earned 2000 total points
ID: 38862045
i didn't expect the output to work, but if you said so, then have u tried modifying your code such that it looks like

  IF SUBSTR(Var1,1,1) = '1'  then do;
    File "h:\xxx\test1.txt";
    writtencnt1+1;
  end;
   else IF SUBSTR(Var1,1,1) = '2' THEN do;
      File "h:\xxx\test2.txt";
      writtencnt2+1;
    end;

and see if it works?
0
 

Author Comment

by:labradorchik
ID: 38869894
Thank you, lowaloysius!
So far I am not getting anything in the counts for writtencnt1 and writtencnt2. Well, I am getting zeros for those both counts which probably means that counts don't work properly.
Could you please check below code and advice what am I doing wrong? May be there is a way to simplify this code somehow?

DATA _NULL_;
 drop readcnt writtencnt1 writtencnt2; 
 infile "allrecords" end=lastrec;
   length readcnt  8 writtencnt1 8 writtencnt2 8;
   retain readcnt  0 writtencnt1 0 writtencnt2 0;
  input @1 Var1 $char20.;

  IF SUBSTR(Var1,1,1) = '1'  then do;
    File "h:\xxx\test1.txt";
    writtencnt1+1;
  end;
   else IF SUBSTR(Var1,1,1) = '2' THEN do;
      File "h:\xxx\test2.txt";
      writtencnt2+1;
    end;

  readcnt+1;
   PUT _INFILE_;

if lastrec then do;
          file mylog.log; 
         
          /***************************
          * Records read from allrecords.dat
          ***************************/
          put "Total records read " readcnt;
         
          /********************************
          * Records written to test1.dat
          ********************************/
          put "Total records written " writtencnt1;
         
          /********************************
          * Records written to test2.dat
          ********************************/
          put "Total records written " writtencnt2;
         
         end;
RUN;

Open in new window

0
 

Author Comment

by:labradorchik
ID: 38876009
lowaloysius, after playing with a different testing data I run the code your way again and it worked fine!
As always, thank you very much!!
0
 
LVL 14

Expert Comment

by:Aloysius Low
ID: 38876118
Good to know that.. sorry been bz so haven't had a chance to look deeper but glad it's working :)
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
How much do you know about the future of data centers? If you're like 50% of organizations, then it's probably not enough. Read on to get up to speed on this emerging field.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Six Sigma Control Plans

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question