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

I'd like to compute &rate across the range of variable 'scale' takes. Data 'want' then would have rate01 thru rate2 across all scales. But my code below produces only one column for a single rate.

Any idea why my code fails to create &rate for whole range?

 

&rate=(agerisk*&scale)/tot_pop;

 

macro want.png

 

data temp;
input tot_pop agerisk;
datalines;
15286	0.055
57522	0.217
28017	0.066
21669	0.064
17853	0.071
17325	0.080
19893	0.081
22637	0.072
22788	0.062
21019	0.048
17881	0.038
11968	0.034
8676	0.031
21670	0.060
;

%symdel;
%macro models(rate,scale);
data want; set temp;
&rate=(agerisk*&scale)/tot_pop;
run;
%mend models;
%models(rate01,0.1);
%models(rate02,0.2);
%models(rate03,0.3);
%models(rate04,0.4);
%models(rate05,0.5);
%models(rate06,0.6);
%models(rate07,0.7);
%models(rate08,0.8);
%models(rate09,0.9);
%models(rate1,1);
%models(rate11,1.1);
%models(rate12,1.2);
%models(rate13,1.3);
%models(rate14,1.4);
%models(rate15,1.5);
%models(rate16,1.6);
%models(rate17,1.7);
%models(rate18,1.8);
%models(rate19,1.9);
%models(rate2,2);
1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

Because you always start from the TEMP data set so no previous changes are maintained. 

Basically you overwrite your results with each iteration, so you need to reuse the data set in this case if you want to include the previous results. 

 

View solution in original post

7 REPLIES 7
Reeza
Super User

Because you always start from the TEMP data set so no previous changes are maintained. 

Basically you overwrite your results with each iteration, so you need to reuse the data set in this case if you want to include the previous results. 

 

Cruise
Ammonite | Level 13
data temp; set temp;
right? so that temp is used repeatedly?
Reeza
Super User

Try it

 


@Cruise wrote:
data temp; set temp;
right? so that temp is used repeatedly?

 

Cruise
Ammonite | Level 13
worked out! Thanks Reeza.
Tom
Super User Tom
Super User

Since your rates are just .1,.2.3,... you could easily do this without macro code.

data want ;
  set temp;
  array rate rate01-rate20;
  do i=1 to dim(rate);
    rate(i) = (agerisk*(i/10))/tot_pop;
  end;
  drop i;
run;
Cruise
Ammonite | Level 13

@Tom

What if rate01-rate20 wasn't known? I eventually would use more granular integers where I have no idea of n points in the range?

Tom
Super User Tom
Super User

Use a separate array for the list of "scale" values.  

So if I had 5 "scale" values I could use a program like this to calculate 5 new "rate" variables.

data want ;
  set temp;
  array rate (5) ;
  array scale (5) _temporary_ (.1 .25 .5 1 1.5) ;
  do i=1 to dim(rate);
    rate(i) = (agerisk*scale(i))/tot_pop;
  end;
  drop i;
run;

You could make it dynamic by putting the list of values into a macro variable and then using COUNTW() to find out how many there were.

%let scales=.1 .25 .5 1 1.5;
%let n=%sysfunc(countw(&scales,%str( )));

So then use the macro variables to define the arrays.

  array rate (&n) ;
  array scale (&n) _temporary_ (&scales) ;

 

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!

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
  • 3068 views
  • 2 likes
  • 3 in conversation