Help using Base SAS procedures

creating new rows. based on existing rows

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 75
Accepted Solution

creating new rows. based on existing rows

Hi, I have a time-series data of the following and I would like to add new id to the table using the existing id. How can i do this? Thank you very much.

date                    ID                    value

.....

want to add:

01/31/1985          0-9               (6201-4183)

02/28/1985          0-9               (6505-4303)

...


Accepted Solutions
Solution
‎04-11-2013 03:59 PM
Super User
Posts: 5,083

Re: creating new rows. based on existing rows

Well, if ID and VALUE are numeric variables, you will be limited as to what you place in there.  But here is an approach.

data want;

   set have;

   by date;

   if first.date then do;

      original_id = id;

      original_value = value;

      retain original_id original_value;

   end;

   output;

   if last.date then do;

      id = 90000 + 100 * original_id + id;

      value = value - original_value;

      output;

   end;

   drop original_id original_value;

run;

Good luck.

View solution in original post


All Replies
Trusted Advisor
Posts: 1,615

Re: creating new rows. based on existing rows

data abc;

    set abc;

    output;

    if id=0 or id=9 then output;

run;

Frequent Contributor
Posts: 75

Re: creating new rows. based on existing rows

this one just duplicated the id number 0 and 9. I would like to create new id 10 that has the different of 0 and 9 as value

Super User
Posts: 10,500

Re: creating new rows. based on existing rows

Is this what you want, assumes ID always has values of 0 through 9 AND value exists for each:

data want;

     set have;

     if ID = 9 then do;

          output;

          ID=10;

          value=dif9(value);

          output;

    end;

     else output;

run;

Solution
‎04-11-2013 03:59 PM
Super User
Posts: 5,083

Re: creating new rows. based on existing rows

Well, if ID and VALUE are numeric variables, you will be limited as to what you place in there.  But here is an approach.

data want;

   set have;

   by date;

   if first.date then do;

      original_id = id;

      original_value = value;

      retain original_id original_value;

   end;

   output;

   if last.date then do;

      id = 90000 + 100 * original_id + id;

      value = value - original_value;

      output;

   end;

   drop original_id original_value;

run;

Good luck.

Frequent Contributor
Posts: 75

Re: creating new rows. based on existing rows

Thanks!.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 193 views
  • 0 likes
  • 4 in conversation