DATA Step, Macro, Functions and more

set the not missing values into new variable and varible name into new variable by using array

Reply
Contributor
Posts: 25

set the not missing values into new variable and varible name into new variable by using array

how to set when not missing values into new variable and varible name into new variable by using array

 

ex: below table

 

IDsubjectpropcordkacrOldvarFiledVal
102aaaasw  propasw
102aaa wewe cordwewe
102aaa  eerkacreer
105bbb ddd cordddd
105bbb  sskacrss
105bbb     
108cccqee  propqee
108cccss  propss
108ccc  asdsakacrasdsa
Valued Guide
Posts: 629

Re: set the not missing values into new variable and varible name into new variable by using array

Posted in reply to SrikanthY

Define an array with the variables prop, cord and kacr. Use a loop to check the array, use vname (or vnamex) to set oldvar, assuming that setting FieldVar is not a problem.

 

Untested code:

data want;
  set have;

  length OldVar $ 32 FiledVal $ 100;
  array values[1:3] prop cord kacr;

  do i = 1 to dim(values);
    if not missing(values[i]) then do;
      OldVar = vnamex(values[i]);
      FiledVal = values[i];
      /* maybe leave loop here */
    end;
  end;
run;
Contributor
Posts: 25

Re: set the not missing values into new variable and varible name into new variable by using array

Posted in reply to andreas_lds
thanks, it's worked.
Super User
Posts: 24,010

Re: set the not missing values into new variable and varible name into new variable by using array

Posted in reply to SrikanthY

1. Define an array

2. Use COALESCEC to find the value, since you only have one per row this is fine. 

3. Use WHICHC to find the index of the variable.

4. Use VNAME to get the name of the variable.

 

Ask a Question
Discussion stats
  • 3 replies
  • 89 views
  • 0 likes
  • 3 in conversation