BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
sasphd
Lapis Lazuli | Level 10

Hello,

Here is my program. As you can see I do a random sample with replacement. I need the sample to be of the same size as the original data. Here I know that the size is equal to 81, so I fixed it. But I want that my program count the number of obsevations automatically. I find that I can use the nbos but I do not know how?

%let numsamp=1000;

%let numobs=81;

data temp04;

do bootsamp=1 to &numsamp;

do bootsamp_obsid=1 to &numobs;

random_seq_obsid=ceil(&numobs*ranuni(1));

output;

end;

end;

run;

1 ACCEPTED SOLUTION

Accepted Solutions
Cynthia_sas
SAS Super FREQ

Hi:

  I think you need to refresh yourself on some Macro basic concepts: https://support.sas.com/resources/papers/proceedings13/120-2013.pdf  &numobs is a macro variable and in your code, think of &numobs as a placeholder. Every time the macro processor sees &numobs, it will type 81 into your program (the current value of the macro variable). In the simplest form, the SAS macro processor is nothing more than a big "search and replace" typewriter. The & references that you see in your program are "resolved" at program compile time. By the time your program goes for execution with data, there are no & references or &macvar references in your program. When I use the term "&macvar", I am using a fake name to illustrate a macro variable being used as a placeholder in code. In your code, you have 2 macro variables: &numobs and &numsamp.

  

The %LET statement is only 1 way of assigning a value to the macro variable &numobs, the SQL step by PGStats is another method. There are a few more. In order to code the %LET statement method, you must know ahead of time the number of observations you want to have used in the program, every time &numobs is found. With PGStats method, you only need to know the name of the dataset whose number of obs you need. Essentially to use PGStats method, you would get rid of the %LET statement for &numobs and replace it with the whole PROC SQL step.

The SAS Macro Facility is very cool and the general rule of thumb is that you start out with a working SAS program before you start to introduce macro variables into the code. But even cooler than macro variables is the ability to have macro programs that can conditionally generate code for you. Take a look at the paper. There are a lot of other good documents on using the SAS Macro facility and once you understand how it works, it will change the way you write SAS programs.

cynthia

View solution in original post

5 REPLIES 5
PGStats
Opal | Level 21

You can save yourself a lot of work with proc surveyselect:

proc surveyselect data=myData out=want method=urs reps=1000 samprate=100 outhits ;

run;

PG

PG
sasphd
Lapis Lazuli | Level 10

Thanks

Yes I know that. I tried it with proc surveyselect and it works with method=urs.

I Wants to do it this way to check something.

data base_orig;

  set man.nv_bd_fonds_individuels10i;

  orig_seq_obsid + 1;

  by crsp_fundno;

  if first.crsp_fundno then orig_seq_obsid = 1;

run;

* Generate 1000 bootstrap samples of random patient IDs;

%let numsamp=1000;

%let numobs=81;

data temp04;

do bootsamp=1 to &numsamp;

do bootsamp_obsid=1 to &numobs;

random_seq_obsid=ceil(&numobs*ranuni(1));

output;

end;

end;

run;

PGStats
Opal | Level 21

You want the total number of observations in your dataset into a macro variable?

proc sql noprint;

select count(*) into :numobs from man.nv_bd_fonds_individuels10i;

quit;

PG

PG
sasphd
Lapis Lazuli | Level 10

Can you please tell me how can I include this in my bootstrap. In place of having numobs=81 (constant), I will make my program count the number of observations for each id and generate 1000 bootstrap samples for each id.

In the example, I suppose that I have id=1 and I find 81 observations. So, by doing the bootstrap I will have totaly 81000 because I need the sample to be of the same size as the original data.

Cynthia_sas
SAS Super FREQ

Hi:

  I think you need to refresh yourself on some Macro basic concepts: https://support.sas.com/resources/papers/proceedings13/120-2013.pdf  &numobs is a macro variable and in your code, think of &numobs as a placeholder. Every time the macro processor sees &numobs, it will type 81 into your program (the current value of the macro variable). In the simplest form, the SAS macro processor is nothing more than a big "search and replace" typewriter. The & references that you see in your program are "resolved" at program compile time. By the time your program goes for execution with data, there are no & references or &macvar references in your program. When I use the term "&macvar", I am using a fake name to illustrate a macro variable being used as a placeholder in code. In your code, you have 2 macro variables: &numobs and &numsamp.

  

The %LET statement is only 1 way of assigning a value to the macro variable &numobs, the SQL step by PGStats is another method. There are a few more. In order to code the %LET statement method, you must know ahead of time the number of observations you want to have used in the program, every time &numobs is found. With PGStats method, you only need to know the name of the dataset whose number of obs you need. Essentially to use PGStats method, you would get rid of the %LET statement for &numobs and replace it with the whole PROC SQL step.

The SAS Macro Facility is very cool and the general rule of thumb is that you start out with a working SAS program before you start to introduce macro variables into the code. But even cooler than macro variables is the ability to have macro programs that can conditionally generate code for you. Take a look at the paper. There are a lot of other good documents on using the SAS Macro facility and once you understand how it works, it will change the way you write SAS programs.

cynthia

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 5 replies
  • 927 views
  • 0 likes
  • 3 in conversation