DATA Step, Macro, Functions and more

Question: Changing number of observations (Macro in bootstrapping/resampling)

Accepted Solution Solved
Reply
Contributor
Posts: 22
Accepted Solution

Question: Changing number of observations (Macro in bootstrapping/resampling)

Hi everyone,

 

I have a time-series of residuals (along with company, date) obtained from the "Reg" procedure, I would like to resample the residual series by the company (each has different number of observations), and make the sample size of each company equal to the number of observations that company has.

 

I would like to use the following example. My question is how do I change that "19" in number of observations into something that changes based on each company.

 

%if &lib = sashelp and &data = class %then %let numobs = 19;

Any suggestion would be greatly appreciated.

 

Thank you very much.


Accepted Solutions
Solution
‎02-04-2018 09:51 PM
PROC Star
Posts: 2,340

Re: Question: Changing number of observations (Macro in bootstrapping/resampling)

Sysfunc simply allows to use SAS functions inside the macro language.

 

Here is full working code:

%macro t;
  %local lib data dsid numobs rc;
  %let lib= SASHELP;
  %let data = CLASS;
  %if &lib = SASHELP and &data = CLASS %then %do;
     %let dsid   = %sysfunc(open(&lib..&data));
     %let numobs = %sysfunc(attrn(&dsid,nobs));
     %let rc     = %sysfunc(close(&dsid     ));
  %end;
  %put &=numobs;
%mend;
%t

NUMOBS=19

So here we open a table, extract NOBS from its metadata and close the table.

 

 

View solution in original post


All Replies
PROC Star
Posts: 2,340

Re: Question: Changing number of observations (Macro in bootstrapping/resampling)

Like this?

%if &lib = SASHELP and &data = CLASS %then %do;
   %let dsid   = %sysfunc(open(&lib..&data));
   %let numobs = %sysfunc(attrn(&dsid,nobs));
   %let rc     = %sysfunc(close(&dsid     ));
%end;
Contributor
Posts: 22

Re: Question: Changing number of observations (Macro in bootstrapping/resampling)

Thank you very much for your reply. Since I still having a hard time applying your codes to mine, I'd really appreciate it if you could kindly elaborate the logic behind the use of sysfunc.
Solution
‎02-04-2018 09:51 PM
PROC Star
Posts: 2,340

Re: Question: Changing number of observations (Macro in bootstrapping/resampling)

Sysfunc simply allows to use SAS functions inside the macro language.

 

Here is full working code:

%macro t;
  %local lib data dsid numobs rc;
  %let lib= SASHELP;
  %let data = CLASS;
  %if &lib = SASHELP and &data = CLASS %then %do;
     %let dsid   = %sysfunc(open(&lib..&data));
     %let numobs = %sysfunc(attrn(&dsid,nobs));
     %let rc     = %sysfunc(close(&dsid     ));
  %end;
  %put &=numobs;
%mend;
%t

NUMOBS=19

So here we open a table, extract NOBS from its metadata and close the table.

 

 

Contributor
Posts: 22

Re: Question: Changing number of observations (Macro in bootstrapping/resampling)

Thank you very much. This is much clearer to me now.
Contributor
Posts: 22

Re: Question: Changing number of observations (Macro in bootstrapping/resampling)

Hi Chris,

The codes worked perfectly. I wonder if this &numobs can be changing for each group, reflecting its separate number of observation?

Thank you very much.
PROC Star
Posts: 2,340

Re: Question: Changing number of observations (Macro in bootstrapping/resampling)

Unsure what you are asking, Like this?

%macro t;
  %local lib data dsid numobs rc;
  %let lib= SASHELP;
  %let data = CLASS;
  %if &lib = SASHELP and &data = CLASS %then %do;
     %let dsid   = %sysfunc(open(&lib..&data(where=(AGE=12))));
     %let numobs = %sysfunc(attrn(&dsid,nlobsf));
     %let rc     = %sysfunc(close(&dsid       ));
  %end;
  %put &=numobs;
%mend;
%t

NUMOBS=5

Contributor
Posts: 22

Re: Question: Changing number of observations (Macro in bootstrapping/resampling)

Exactly. Thank you very much!
☑ This topic is solved.

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

Discussion stats
  • 7 replies
  • 137 views
  • 3 likes
  • 2 in conversation