BookmarkSubscribeRSS Feed
jerry898969
Pyrite | Level 9

I have a data table with 5 vars that I want to do some work with.  The names of these vars are Sec1-Sec5

Sec1 Sec2 Sec3 Sec4 Sec5

Test1 Test2 Test3         Test5

Test1

         Test2          Test4

With the above data I need to create a field that will tell me which fields are populated.

I want my end result to look like this

1,2,3,5

1

2,4

What is the best way to approach this?

Thank you

3 REPLIES 3
JerryLeBreton
Pyrite | Level 9

A little array processing is one way to solve this...

data want;

  set have;

  array Secs[5] sec1-sec5;

  length populated $10;

  do i = 1 to 5;

     if secs ne '' then

       populated = catx(',', populated, put(i,1.));

  end;

  drop i;

run;



PGStats
Opal | Level 21

Or, adding a few tricks of the trade (learned mostly on this Forum) :

data want;

set have;

array S{*} sec:;

length populated $10;

do i = 1 to dim(S);

     if not missing(S{i}) then

          populated = catx(',', populated, i);

     end;

drop i;

run;

PG

PG
Jagadishkatam
Amethyst | Level 16

alternatively,

data want;

    set have;

    populated=compress(catx(',',of sec1-sec5),,'a');

run;

Thanks,

Jagadish

Thanks,
Jag

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 781 views
  • 0 likes
  • 4 in conversation