BookmarkSubscribeRSS Feed
SrikanthY
Calcite | Level 5

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
3 REPLIES 3
andreas_lds
Jade | Level 19

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;
Reeza
Super User

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.

 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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
  • 3 replies
  • 723 views
  • 0 likes
  • 3 in conversation