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

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.

1 ACCEPTED SOLUTION

Accepted Solutions
ChrisNZ
Tourmaline | Level 20

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

7 REPLIES 7
ChrisNZ
Tourmaline | Level 20

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;
KrisDeng
Obsidian | Level 7
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.
ChrisNZ
Tourmaline | Level 20

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.

 

 

KrisDeng
Obsidian | Level 7
Thank you very much. This is much clearer to me now.
KrisDeng
Obsidian | Level 7
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.
ChrisNZ
Tourmaline | Level 20

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

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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