简体   繁体   中英

SAS: Creating a line of new variables within a macro

I have this variable x_yyww , for every week the past 5 years. It can take the value 0, 1 or blank. I want to create a new line of variables which takes the value yyWww if the combination 1 and then 0 occurs, eg if x_0449=1 and x_0450=0 . I then need to create this new set of variables, call them ovg_1 - ... ovg_n , which shows the specific date yyWww (04W50) when the event occurred, where n is the maximal number of times this combination has occurred for a given person. This is where I need help. When I try to code this in SAS, i end up with all the variables ovg_1 ... ovg_n only taken the most recent value. Since my skills clearly are limited, I have been trying to do this with a fixed n=25 ...

   %macro test(h) ;

...

   %do    i=2    %to     52;
     %let     j=%eval(&i-1);
       %if    &j    lt    10     %then    %let    j=0&j;
      %let    l=%eval(&i);
         %if    &l    lt     10    %then    %let    l=0&l;

....

  %do     n=1    %to    25

  if     x_&h&l="0"     and     x_&h&j="1"     then    do;
     ovg_&n = intnx('week.1',   mdy(1, 1, &h),   &l);     end;

  format    ovg_&k weekv5. ;

....

Can anyone help me out on this? (I know I am missing some endpoint by this, and I have taken care of it in my original coding)

Try something like this:

/* create some test data */
data test(drop=j);
retain starts_at '01JAN1990'd; *** date event collection starts;
array x_{*} event1 - event1000;  *** events;
array y_{*} date1 - date1000;  *** date of events;
do person=1 to 10;
    do j=1 to dim(x_); *** generate 1000 0s and 1s;
        x_{j}=round(ranuni(person));
        y_{j}=.;
    end;
    n=0;
    do j=1 to dim(x_)-1; *** count 0s followed by 1s;
        if x_{j}=0 and x_{j+1}=1 then do;
            n+1;
            y_{n}=starts_at+(j*7); *** calculate the date;
        end;
    end;
    output;
    format date1-date1000 weekv5.;
end;

run;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM