Help using Base SAS procedures

Sasdataset

Accepted Solution Solved
Reply
Contributor
Posts: 36
Accepted Solution

Sasdataset

Hi all,

I am trying to create a new dataset where when i run the code for the first time i should read only first record from the dataset and when i run the code for the second time i should read the second record from the dataset instead of the first rercord. I am trying find a way by changing the firstobs value after every run. So , is there any way to get the result in such a way. 

For example :

data test1;

set test (firstobs=1 obs=1);

run;

I will have first record in test1

when run this dataset again

data test1;

set test (firstobs=2 obs=1);

run;

i will be having first and second record in the dataset . In a similar way if i run the dataset for the third time i should have first, second and third in the dataset test1

Thank in advance. Your help will be appreciated.


Accepted Solutions
Solution
‎02-01-2012 11:11 AM
SAS Super FREQ
Posts: 8,743

Re: Sasdataset

Hi:

  Have you tested your theory about how firstobs and obs work, with actual code? Based on the code you posted, you should be seeing some errors in the log:

652  data test1;
653  set sashelp.class (firstobs=1 obs=1);
654  run;
  
NOTE: There were 1 observations read from the data set SASHELP.CLASS.
NOTE: The data set WORK.TEST1 has 1 observations and 5 variables.

   

655
656  data test2;
657  set sashelp.class (firstobs=2 obs=1);
ERROR: FIRSTOBS option > OBS option - no data to read from file SASHELP.CLASS.
658  run;
  
NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set WORK.TEST2 may be incomplete.  When this step was stopped there were 0 observations and 0 variables.

  

659
660  data test3;
661  set sashelp.class (firstobs=3 obs=1);
ERROR: FIRSTOBS option > OBS option - no data to read from file SASHELP.CLASS.
662  run;
  
NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set WORK.TEST3 may be incomplete.  When this step was stopped there were 0 observations and 0 variables.

If you look in the doc, you will find that FIRSTOBS says where to START reading and OBS tells SAS which observation to use as the stopping point. So when you specified FIRSTOBS=2 and OBS=1, you were essentially SAS to start reading at observation #2, but to stop reading at observation #1, and, as the ERROR message indicates, that is a problem.

As for how to generate repetitive code, you would look at doing some form of SAS Macro processing. But you need to start with a working SAS program, first. After you have a working program, you might want to take a look at this paper (http://www2.sas.com/proceedings/sugi28/056-28.pdf) that outlines some of the basics of SAS Macro processing.

cynthia

Suggested code below:

** suggested code;

** start at 1, read obs 1;

data alt1;

set sashelp.class (firstobs=1 obs=1);

run;

  

** start at 1, read obs 1 and 2, stop at 2;

data alt2;

set sashelp.class (firstobs=1 obs=2);

run;

  

** start at 1, read 1, 2 and 3, stop at 3;

data alt3;

set sashelp.class (firstobs=1 obs=3);

run;

  

** start at 4, read to the end of the file (total 16);

** since SASHELP.CLASS has 19 obs;

data alt4;

set sashelp.class(firstobs=4 obs=max);

run;

added link to macro paper

View solution in original post


All Replies
PROC Star
Posts: 7,363

Sasdataset

You just have a misunderstanding of obs.  Try the following:

data test1;

  set sashelp.class (firstobs=1 obs=1);

run;

data test2;

  set sashelp.class (firstobs=2 obs=2);

run;

Respected Advisor
Posts: 3,124

Re: Sasdataset

One thing I can think of is to use macro:

%macro test;

data _null_;

if 0 then set sashelp.class nobs=nobs;

call symput ('n',nobs);

run;

%do i=1 %to &n;

data want&i;

set sashelp.class (obs=&i);

run;

%end;

%mend;

%test

Kindly Regards,

Haikuo

Solution
‎02-01-2012 11:11 AM
SAS Super FREQ
Posts: 8,743

Re: Sasdataset

Hi:

  Have you tested your theory about how firstobs and obs work, with actual code? Based on the code you posted, you should be seeing some errors in the log:

652  data test1;
653  set sashelp.class (firstobs=1 obs=1);
654  run;
  
NOTE: There were 1 observations read from the data set SASHELP.CLASS.
NOTE: The data set WORK.TEST1 has 1 observations and 5 variables.

   

655
656  data test2;
657  set sashelp.class (firstobs=2 obs=1);
ERROR: FIRSTOBS option > OBS option - no data to read from file SASHELP.CLASS.
658  run;
  
NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set WORK.TEST2 may be incomplete.  When this step was stopped there were 0 observations and 0 variables.

  

659
660  data test3;
661  set sashelp.class (firstobs=3 obs=1);
ERROR: FIRSTOBS option > OBS option - no data to read from file SASHELP.CLASS.
662  run;
  
NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set WORK.TEST3 may be incomplete.  When this step was stopped there were 0 observations and 0 variables.

If you look in the doc, you will find that FIRSTOBS says where to START reading and OBS tells SAS which observation to use as the stopping point. So when you specified FIRSTOBS=2 and OBS=1, you were essentially SAS to start reading at observation #2, but to stop reading at observation #1, and, as the ERROR message indicates, that is a problem.

As for how to generate repetitive code, you would look at doing some form of SAS Macro processing. But you need to start with a working SAS program, first. After you have a working program, you might want to take a look at this paper (http://www2.sas.com/proceedings/sugi28/056-28.pdf) that outlines some of the basics of SAS Macro processing.

cynthia

Suggested code below:

** suggested code;

** start at 1, read obs 1;

data alt1;

set sashelp.class (firstobs=1 obs=1);

run;

  

** start at 1, read obs 1 and 2, stop at 2;

data alt2;

set sashelp.class (firstobs=1 obs=2);

run;

  

** start at 1, read 1, 2 and 3, stop at 3;

data alt3;

set sashelp.class (firstobs=1 obs=3);

run;

  

** start at 4, read to the end of the file (total 16);

** since SASHELP.CLASS has 19 obs;

data alt4;

set sashelp.class(firstobs=4 obs=max);

run;

added link to macro paper

Contributor
Posts: 36

Re: Sasdataset

Thank you all for you replies.

I am not clear about what results i would like to see but thank you all. I will go through the macro paper too.

Contributor
Posts: 36

Sasdataset

Like in cobol  i have to read a record from the dataset(let it be dataset = test) and do some maniplations and if it meets the requirment then write the record to the new dataset (let it be test1) and then using proc sql the record obtained from new dataset (test1)  will be inserted into another dataset (test2) . Again if i have go to statements in the later part of the program , i might need to read the next record from the dataset test, i mean the second but not the first record from the dataset test and in a similar way if it pass through the conditions then dataset (test1) will have this record and will be inserted into dataset test2. So, Initially i am reading only first record from dataset test. Hope my issue is clear. Thak you.

SAS Super FREQ
Posts: 8,743

Re: Sasdataset

Hi:

  My COBOL days are long behind me. However, in SAS, when you read data (whether with a SET statement or an INFILE statement), SAS reads the INPUT file or data set one observation or "record" at a time, over and over again, in an implied loop until the end of file marker or end of data set marker is reached. We would normally discourage the use of "GO TO" type of logic or statements for explicitly forcing a read, unless you had advanced or unusual circumstances.

  For example, in the program below, SASHELP.CLASS is a data set that has 19 records or observations. I want to calculate the projected height in 5 years, and age in 5 years for each of the 19 students. So, in my program, the combination of DATA statement and SET statement instructs SAS to start reading and loading data from the "top" of the file, deal with the first manipulation, creating the HT_IN_5 and AGE_IN_5 variables, and then at the end of the program (before the RUN statement), I have an explicit OUTPUT statement, which causes SAS to write the record. After SAS is done processing that first observation and writing it to the new WORK.ALLSTUDENTS data set, SAS asks whether there are any more observations in the file....well, SASHELP.CLASS has 19 observations and SAS has only finished with the first observation, so it is NOT the end of SASHELP.CLASS and there ARE more observations in the input data set, so SAS goes back and automatically loads the second observation into a "buffer" area called the Program Data Vector. Then, SAS operates on the second observation and outputs it to the new data set. Then, this process continues until all the observations are loaded, manipulated and output to the new data set. In this way, you can see that the DATA step program operates like an "implicit" loop.

  If you run the program below, you will see how the original file (SASHELP.CLASS) compares to the newly created file (WORK.ALLSTUDENTS). I did not need to force SAS to go through the observations in SASHELP.CLASS -- that happened automatically.

  A very good description of the DATA step and implied loop is this paper by Ian Whitlock (http://www2.sas.com/proceedings/sugi31/246-31.pdf). If you like flowcharts, this documentation topic (http://support.sas.com/documentation/cdl/en/lrcon/62753/HTML/default/viewer.htm#p08a4x7h9mkwqvn16jg3...), entitled "Overview of Data Step Processing" contains a chart that shows the flow of action in a DATA step program. And, this (http://listserv.uga.edu/cgi-bin/wa?A2=ind9906d&L=sas-l&P=9399) older SAS-L post contains a chart that shows the differences between various COBOL statements and SAS statements. My only quibble with the post is that the quotes apparently got displayed incorrectly so where you see something like ?A?, I believe it was meant to show either 'A' or "A" (in other words, ? characters should be ' or ").

cynthia

ods listing close;

  

ods html file='c:\temp\original_table.html';

proc print data=sashelp.class;

  title '1) Original TABLE SASHELP.CLASS';

run;

ods html close;

 

data allstudents(keep=name age height age_in_5 ht_in_5);

  set sashelp.class;

  ** calculate height in 5 years;

  ** based on growing 10%;

  age_in_5 = age + 5;

  ht_in_5 = height * 1.10;

  output allstudents;

run;

   

ods html file='c:\temp\new_table.html';

proc print data=allstudents;

  title '2) New Table with New Variables';

run;

ods html close;

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 1160 views
  • 6 likes
  • 4 in conversation