Help using Base SAS procedures

First.variable and last.variable

Reply
Regular Contributor
Posts: 229

First.variable and last.variable

data countynm1;
input Obs COUNTRY$:10. READING;
cards;
1 LAND 125
2 LAND 611
3 BAY 101
4 BAY 101
5 BAY 222
6 WASHINGTON 143
run;


I want like this
1. Create a separate dataset for each reporting country.
2. Produce a separate PROC PRINT for each reporting county.

Message was edited by: Main Message was edited by: Main
N/A
Posts: 0

Re: First.variable and last.variable

Surely this is a forum for asking questions not simply demanding solutions.
Regular Contributor
Posts: 229

Re: First.variable and last.variable

Posted in reply to deleted_user
HI i have quoted my question not demanded as i framed that sentence like this as i quoted the query that is the way i want the answer .i also know this is a forum no on demand .If u know Reply to it.
N/A
Posts: 0

Re: First.variable and last.variable

There is no question in your post.
Super Contributor
Super Contributor
Posts: 3,174

Re: First.variable and last.variable

Suggest you explore using the SAS DATA step technique with multiple OUTPUTs, one for each file, and as you mentioned, using IF FIRST. to increment a count variable. Then use the SAS OUTPUT statement to send an observation to an output file name based on the incrementing variable. Then code your PROC PRINT steps, one for each split-file.

Another option is using SAS MACRO language concept with a macro coded using %DO and %END; to process your input records and generate "n" output files. A list of COUNTRY values can be generated with PROC SQL, generating a list of SAS macro variables, for which you would process each in your macro to create a split-file, and then print it.

The SAS support website http://support.sas.com/ has SAS hosted documentation, and also there are technical papers on this type of topic. Suggest you consider using the SEARCH facility at the website to identify references related to your post and also check the archives for this forum.

Here is one of the technical conference papers I found using the SAS support website SEARCH facility:


WHAT WOULD I DO WITHOUT PROC SQL AND THE MACRO LANGUAGE
Jeff Abolafia, Rho, Inc., Chapel Hill, NC

http://www2.sas.com/proceedings/sugi30/031-30.pdf


SAS MACRO language -- Using PROC SQL with the SAS Macro Facility

http://support.sas.com/documentation/cdl/en/sqlproc/62086/HTML/default/a001360983.htm

-- Creating and Using Macro Variables
http://support.sas.com/documentation/cdl/en/sqlproc/62086/HTML/default/a002595788.htm


Suggest developing a SAS program to accomplish your objective, re-post a reply with the code, for feedback from the forum.

Scott Barry
SBBWorks, Inc.
SAS Super FREQ
Posts: 8,868

Re: First.variable and last.variable

Posted in reply to Proccontents
Hi,
In addition to other comments, it's also hard to imagine why you want #1 (a separate data set for every country) when you can achieve #2 with a simple PROC PRINT and BY group processing. Assuming the file is sorted by country, then you can simply use BY statements with NEWFILE=BYGROUP (in HTML, RTF or PDF and some few other destinations. You'd need PAGEBY in the LISTING destination to get every country on a separate page).

The reason I found your post confusing was that if ALL you wanted was a separate dataset for every country so you could do a PROC PRINT, there would be no need for tracking first.byvar or last.byvar unless you needed to ALSO reset some counter variable or accumulator variable for each country OR you wanted to only output 1 observation per country. So, that piece of your post just did not make sense to me.

cynthia

[pre]
ods listing;
ods html file='c:\temp\county1.html' style=sasweb
newfile=bygroup;
title 'New Proc Print for Every ByGroup';
proc print data=countynm1;
by country;
pageby country;
var country reading;
sum reading;
run;
ods html close;
[/pre]
Regular Contributor
Posts: 229

Re: First.variable and last.variable

DATA _NULL_;
SET COUNTYDT END=EOF; /* READ SAS DATASET */
BY COUNTYNM; /* SORT SEQ */
IF FIRST.COUNTYNM THEN DO; /* NEW COUNTY ? */
NUMCTY+1; /* ADD 1 TO NUMCTY */
CTYOBS=0; /* OBS PER COUNTY TO 0 */
END;
CTYOBS+1; /* ADD ONE OBSER FOR CTY */
IF LAST.COUNTYNM THEN DO; /* EOF CTY, MAKE MAC VARS*/
CALL SYMPUT('MCTY'||LEFT(PUT(NUMCTY,3.)),COUNTYNM);
CALL SYMPUT('MOBS'||LEFT(PUT(NUMCTY,3.)),LEFT(CTYOBS));
END;
IF EOF THEN
CALL SYMPUT('MTOTCT',NUMCTY); /* MAC VAR NO DIF CTYS */
RUN;
%PUT *** MTOTCT=&MTOTCT; /* DISPLAY NO OF CTYS */


%MACRO COUNTYMC; /* MACRO START */
%DO I=1 %TO &MTOTCT; /* LOOP THRU ALL CTYS */
%PUT *** LOOP &I OF &MTOTCT; /* DISPLAY PROGRESS */
PROC PRINT DATA=COUNTYDT; /* PROC PRINT */
WHERE COUNTYNM="&&MCTY&I"; /* GENERATED WHERE */
OPTIONS PAGENO=1; /* RESET PAGENO */
TITLE "REPORT FOR COUNTY &&MCTY&I";
/* TITLES AND FOOTNOTES */
FOOTNOTE "TOTAL OBSERVATION COUNT WAS &&MOBS&I";
RUN;
%END; /* END OF %DO */
%MEND COUNTYMC; /* END OF MACRO */
%COUNTYMC /* INVOKE MACRO */ Message was edited by: Main
Ask a Question
Discussion stats
  • 6 replies
  • 211 views
  • 0 likes
  • 4 in conversation