BookmarkSubscribeRSS Feed
ToRsy
Fluorite | Level 6

I'd like to replicate and modify specific rows in the table.

before:

xyz_id | letter | Col_1 | ...|
1 | Z | V1 | ... |
2 | Z | V2 | ... |
3 | Z | V3 | ... |

after:

xyz_id | letter | Col_1 | ...|
1 | A | V1.1 | ... |
1 | B | V1.2 | ... |
1 | C | V1.3 | ... |
2 | A | V2.1 | ... |
2 | B | V2.2 | ... |
2 | C | V2.3 | ... |
3 | A | V3.1 | ... |
3 | B | V3.2 | ... |
3 | C | V3.3 | ... |

I've prepared the following code:

data test2;
        set test;

        
        array letters {3} $3 _temporary_ ('A', 'B', 'C');
        array weights {3}  _temporary_ (1,2,3);

/*      if xyz_id = '1' */
/*          then  array weights {3}  _temporary_ (1,2,3);*/
/*      else if xyz_id = '2'*/
/*          then array weights {3}  _temporary_ (8,7,6);*/
/*      else array weights {3}  _temporary_ (1,1,1)*/


        do i = 1 to 8;
            letter = letters(i);
            Col_A = Col_A * weights(i);
            output;
        end;
          drop i;
run;

Now, I'm trying to make weights depend of the letter column (commented code) - but without any success. I also tried:

   array weights_1 {3}  _temporary_ (1,2,3);
   if xyz_id = '1'
            then  weights = weights_1;

but it didn't work as well.

Any suggestions?

2 REPLIES 2
Kurt_Bremser
Super User

An array is a static object defined once and created during the data step compilation phase. You cannot create it conditionally (unless you generate the code conditionally with macro language).

You can create a large array with all weights, and then set the index conditionally depending on a variable value.

Tom
Super User Tom
Super User

If the weight depends on the ID then why not just put them into another dataset instead of storing the weights in the code?

data have;
  input xyz_id letter $ Col_1 col_2;
cards;
1 Z 2 1
2 Z 3 2
3 Z 4 3
;

data weights;
  input xyz_id weight1-weight3 ;
cards;
1 1 2 3
2 8 7 6
3 1 1 1
;

data want;
  merge have weights ;
  by xyz_id;
  array weights weight1-weight3 ;
  array letters [3] $3 _temporary_ ('A' 'B' 'C');
  array old col_1-col_2;
  array new new_1-new_2;
  do i=1 to dim(letters);
    letter=letters[i];
    do j=1 to dim(old);
      new[j] = old[j] * weights[i];
    end;
    output;
  end;
run;

proc print;
run;

PS It is just as easy to type the data step to create the data as it is to type those tables with the |'s in them.  And if you use the Insert SAS Code icon so you get a fixed font it is just as easy to read. The |'s just limit the amount of information you can put on a line.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 533 views
  • 1 like
  • 3 in conversation