DATA Step, Macro, Functions and more

Using a Data Null to output a report. Using 2 input datasets, can't get the output I want!

Reply
New Contributor
Posts: 2

Using a Data Null to output a report. Using 2 input datasets, can't get the output I want!

Hi! Trying something a little different from what I normally do with data nulls for our CSV outputs. Trying to output 2 datasets in one output. Its kind of works but isn't inserting the break or secondary headings I want. Any help most welcome.

 

Heres the code :-

 

 

DATA _NULL_;

		SET work.agebs (IN = FREQ) 
			work.agebsout (IN = OUTP) END = last;

		FILE &csvfile1 linesize=1000  lrecl=1000;

IF FREQ THEN DO;

IF _n_ = 1 THEN
        DO ;

		PUT "FREQUENCIES FOR AGEBS,,,&year";
		PUT ",,";
		PUT "AGE,COUNT,PERCENTAGE";
		PUT ",,";

		END;

		IF AGE = . THEN PUT

		'Missing,'
		COUNT ','
		PERCENT 7.2;

		ELSE PUT

		AGE ','
		COUNT ','
		PERCENT 7.2;


END;



IF OUTP THEN DO;


IF row = 1 THEN
        DO ;

		PUT "Records with missing age or age out of range of 16­90";
		PUT ",,";
		PUT "Date of Birth,Date Now,Age,ID,Calculated Age,";
		PUT ",,";
		END;

		PUT DOB  ','
			DONOW   ','
			AGE ','
			ID  ','
			calcage;
END;
	


RUN;

 

 

1st is what I want out (appended but enough to give an idea, Ignore numbers all been changed just illustrative)

2nd is what I currently get out

FREQUENCIES FOR AGE 2012 
     
AGECOUNTPERCENTAGE 
     
Missing20.01  
48440.03  
61770.08  
78880.37  
81990.68  
     
     
Records with missing age or age out of range of 16-­90
     
Date of BirthDate of MarriageAGEIDCalculated Age
     
21/02/197120/10/2015.144
  21/10/2015.2.
  22/10/2015.3.
17/04/198823/10/2015.427
12/07/198924/10/2015.526

 

FREQUENCIES FOR AGEBS 2012 
     
AGECOUNTPERCENTAGE 
     
Missing20.01  
48440.03  
61770.08  
78880.37  
81990.68  
21/02/197120/10/2015.144
  20/10/2015.2.
  20/10/2015.3.
17/04/198820/10/2015.427
12/07/198920/10/2015.5

26

    

 

Regular Contributor
Posts: 212

Re: Using a Data Null to output a report. Using 2 input datasets, can't get the output I want!

Posted in reply to ScottyEgg

Hi mate,

 

I think if you change the second part of your code to the following, it will work:

 

IF _n_ = 1 THEN
        DO ;
 
		PUT "Records with missing age or age out of range of 16­90";
		PUT ",,";
		PUT "Date of Birth,Date Now,Age,ID,Calculated Age,";
		PUT ",,";
		END;

		PUT DOB  ','
			DONOW   ','
			AGE ','
			ID  ','
			calcage;

Change the row = to _n_ = 

 

Att,

New Contributor
Posts: 2

Re: Using a Data Null to output a report. Using 2 input datasets, can't get the output I want!

Posted in reply to DartibaliRodrigo
Made that change and it still seemed to fall over (think as it was using 2 datasets, the first observation in the 2nd dataset wasn't being being treated as "_N_ = 1"
Have now added a step to add row numbering to the datasets and it seems to work!
Probably a little long winded!
Frequent Contributor
Posts: 144

Re: Using a Data Null to output a report. Using 2 input datasets, can't get the output I want!

[ Edited ]
Posted in reply to DartibaliRodrigo

Hi ScottEgg,

 

you have used 

IF ROW=1 THEN DO

Your input dataset have ROW variable? If not, you should do that your second dataset have ROW=_N_; Doing that, you will have a kind of '_N_' for the second dataset

Super User
Super User
Posts: 7,942

Re: Using a Data Null to output a report. Using 2 input datasets, can't get the output I want!

Posted in reply to ScottyEgg

Personally I think your over complicating things for yourself.  FIrst work on getting a dataset which looks as you want it, then output it.  There is no reason to do it all in one step.  Secondly, your output file is not a CSV, it is a format you have come up with yourself, maybe based in some way on CSV, but its not.  CSV file has one row of header labels separated by comma, then and until the EOF data for those columns separated by commas.  You have two separate files in one.  If you require output, then go directly to RTF or PDF and put each on separate pages, for data, keep the data, don't try to keep the output in some form of data?

 

 

Ask a Question
Discussion stats
  • 4 replies
  • 234 views
  • 0 likes
  • 4 in conversation