DATA Step, Macro, Functions and more

visit and ranges

Reply
Contributor
Posts: 21

visit and ranges

Dear All,

 

I would like to create a dataset with vist and ranges.  some where i am getting wrong. Any insight or better approach will be helpful

 

 

%let week=1 3 7 11 15 17 18 19 20 23 27 31 35 39 43 48 52;
%let winlow=0 2 37 65 93 114 124 131 138 152 177 205 233 261 289 320 352;
%let winhi=1 36 64 92 113 123 130 137 151 176 204 232 260 288 319 351 370;
%macro win;
   %do i=1 %to %sysfunc(countw(&week));
   %let vis=%scan(&week,&i); %let low=%scan(&winlow,&i); %let hi=%scan(&winhi,&i);
   data t;
        vis=&vis;winlow=&low;winhi=&hi;
   run;
   %end;
   proc append base=visrange data=t force;run;
 %mend;
 %win;

 

 

Thanks

Cathy

 

Super User
Posts: 17,745

Re: visit and ranges

The location of your loop doesn't make sense. Remember that macro just generates code. 

 

So at i=1 you get:

 

  data t;
        vis=1;winlow=0;winhi=1;
   run;

 

But at i=2 you get

  data t;
        vis=3;winlow=2;winhi=36;
   run;

 

However this is within the same loop so you've overwritten your data set t, and your append doesn't do much. 

If you want to fix it move the %end to after your proc append, not after data step.

 

I would suggest moving this all into a data step without macro/macro loops. This is untested. Make sure the variables are long enough to hold your macro variable list.

 

  data t;
   length vis_list winlow_list winhi_list $2000.;
    vis_list = "&vis";
    winlow_list = "&winlow";
    winhi_list = "&winhi";

    n_count = countw(vis_list);

   do i=1 to n_count;
        week=scan(vis_list, i);
        win_low = scan(winlow_list, i);
        winhi_list = scan(winhi_list, i);
        OUTPUT;
    end;

   run;

 

Respected Advisor
Posts: 4,640

Re: visit and ranges

There is certainly a simpler way to achieve this. What is the link between week and winLow?

PG
Contributor
Posts: 21

Re: visit and ranges

hi pgstat,

 

I have a variable days in dataset. and in data presentation plan, we have ranges , if days fall under these days i need to create a week and visit variables

 

for example : in data presentation plan

%let winlow=0 2 37 65 93 114 124 131 138 152 177 205 233 261 289 320 352;
%let winhi=1 36 64 92 113 123 130 137 151 176 204 232 260 288 319 351 370;

 

and a days variable like 1,22,50,7,106,120,127,134,141,162,190,218,246,274,302,337,360

 

need to create week and visitnum based on the ranges

 

Thanks

Cathy

 

 

Respected Advisor
Posts: 4,640

Re: visit and ranges

If the problem is to translate day numbers into ranges, you could do something like this:

 

data days;
input day @@;
datalines;
1 22 50 7 106 120 127 134 141 162 190 218 246 274 302 337 360
;

data t;
array lo{20} _temporary_ 
(0 2 37 65 93 114 124 131 138 152 177 205 233 261 289 320 352);
array hi{20} _temporary_ 
(1 36 64 92 113 123 130 137 151 176 204 232 260 288 319 351 370);
array w{20} _temporary_
(1 3 7 11 15 17 18 19 20 23 27 31 35 39 43 48 52);
set days;
do weekNum = 1 by 1 while(day > hi{weekNum}); end;
week = w{weekNum};
weekLow = lo{weekNum};
weekHi = hi{weekNum};
run;

proc print; run;
PG
Ask a Question
Discussion stats
  • 4 replies
  • 230 views
  • 0 likes
  • 3 in conversation