BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
walterwang
Obsidian | Level 7

I want to get one part of the data and calculate every time. for example, first time obs: 1 to 14, second time obs: 15 to 28 etc.

I  use a macro

 

%macro agestd_one(number);

%do i=1 %to &number %by 14;

 

data import;

set T0314(firstobs=&i obs=14);

 

run;

%end;

%mend;

 

I run: 

%agestd_one(14);

works well.

 

I run:

%agestd_one(28);

 

error message:

ERROR: FIRSTOBS option > OBS option - no data to read from file WORK.T0314.

 

what wrong at here?

 

 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

A suggestion for your macro:

%macro agestd_one(number);
%do start=1 %to &number %by 14;
  %let end=%eval(&start+13);

data import;
set T0314 (firstobs=&start. obs=&end.);
run;

%end;
%mend;

View solution in original post

4 REPLIES 4
Kurt_Bremser
Super User
obs=

has to contain the physical number of the last observation to be read, not the number of observations you want to read

firstobs=15 obs=28

will read 14 observations, not 28.

ChrisNZ
Tourmaline | Level 20

@Kurt_Bremser we really need an alias  lastobs=  for the  obs=  option.

Kurt_Bremser
Super User

You fell into a violation of the basic macro development principle: start with working code.

If you had tried

firstobs=28 obs=14

in Base SAS without the macro around it, you would have encountered the problem there, and not thought that your macro code is the culprit.

Kurt_Bremser
Super User

A suggestion for your macro:

%macro agestd_one(number);
%do start=1 %to &number %by 14;
  %let end=%eval(&start+13);

data import;
set T0314 (firstobs=&start. obs=&end.);
run;

%end;
%mend;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1326 views
  • 5 likes
  • 3 in conversation