简体   繁体   中英

Split a row into multiple rows in SAS enterprise guide

I need help to split a row into multiple rows when the value on the row is something like 1-5. The reason is that I need to count 1-5 to become 5, and not 1, as it is when it count on one row.

I've a ID, the value and where it belong.

As exempel:

ID  Value Page
1    1-5   2

The output I want is something like this:

ID Value Page
1    1    2
1    2    2
1    3    2
1    4    2
1    5    2

I've tried using a IF-statement

IF bioVerdi='1-5' THEN
        DO;
            ..
        END;

So I don't know what I should put between the DO; and END;. Any clues to help me out here?

You need to loop over the values inside your range and OUTPUT the values. The OUTPUT statement causes the Data Step to write a record to the output data set.

data want;
set have;
if bioVerdi = '1-5' then do;
   do value=1 to 5;
      output;
   end;
end;

Here is another solution that is less restricted to the actual value '1-5' given in your example, but would work for any value in the format '1-6', '1-7', '1-100', etc.

*this is the data you gave ;
data have ; 
    ID = 1 ; 
    value = '1-5';
    page = 2;
run;

data want ; 
 set have ; 

 min = scan( value, 1, '-' ) ; * get the 1st word, delimited by a dash ;
 max = scan( value, 2, '-' ) ; * get the 2nd word, delimited by a dash ;

 /*loop through the values from min to max, and assign each value as the loop iterates to a new column 'NEWVALUE.' Each time the loop iterates through the next value, output a new line */
 do newvalue = min to max ;
    output ; 
 end;

 /*drop the old variable 'value' so we can rename the newvalue to it in the next step*/
 drop value min max; 

 /*newvalue was a temporary name, so renaming here to keep the original naming structure*/
 rename newvalue = value ; 

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