BookmarkSubscribeRSS Feed
cathy_sas
Calcite | Level 5

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

 

4 REPLIES 4
Reeza
Super User

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;

 

PGStats
Opal | Level 21

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

PG
cathy_sas
Calcite | Level 5

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

 

 

PGStats
Opal | Level 21

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

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1670 views
  • 0 likes
  • 3 in conversation