BookmarkSubscribeRSS Feed
vikram_e
Fluorite | Level 6

Hi There,

 

I am having set of variables (trt01 - trt07) having character values , i need to find maximum non missing treatment from 7 treatments group.i need to use that last 2 digit number in a macro variable. 

 

            trt01   trt02   trt03   trt04                     

1          abc               def               ----------->   trt03

2          abc                                    ----------->   trt01

3         abc    def      ght    dsa      ------------>   trt04

 

 

any one help me please.

 

3 REPLIES 3
vikram_e
Fluorite | Level 6

Hi There,

 

I am having set of variables (trt01 - trt07) having character values , i need to find maximum non missing treatment from 7 treatments group.i need to use that last 2 digit number in a macro variable. 

 

            trt01   trt02   trt03   trt04                     

1          abc               def               ----------->   trt03

2          abc                                    ----------->   trt01

3         abc    def      ght    dsa      ------------>   trt04

 

instead of writing group of if then else statement any other dynamic way to find treatment ,any one help me please.

 

data d2;
set d1
array trt(7)  $12.;
array trt_n(7);

do i = 1 to dim(trt);
trt_n(i)= ^missing(trt(i));
x = max(reverse(substr(cmtrt_atcn(i),1,2)));
L_trt = trt||x; end; run;

 

error_prone
Barite | Level 11

@vikram_e wrote:

Hi There,

 

I am having set of variables (trt01 - trt07) having character values , i need to find maximum non missing treatment from 7 treatments group.i need to use that last 2 digit number in a macro variable. 

 

            trt01   trt02   trt03   trt04                     

1          abc               def               ----------->   trt03

2          abc                                    ----------->   trt01

3         abc    def      ght    dsa      ------------>   trt04

 

 

any one help me please.

 


Can you explain what should be stored in the macro-variable after all obs have been processed?

 

To find the last treatment use an array and a loop with decreasing index variable.Something like (untested code😞

array t trt01-trt04;

do i = dim(t) to 1 by -1;
  if not missing(t[i]) then do;
    max = vname(t[i]);
    leave;
  end;
end;

 

 

data_null__
Jade | Level 19
data x;
   infile cards missover;
   input id (trt01-trt07) ($);
cards;
1   abc   .  def               
2   abc                        
3   abc    def      ght    dsa 
;;;;
   run;
data x2;
   set x;
   array _trt[*] trt07-trt01;
   i  = whichC(coalesceC(of _trt[*]),of _trt[*]);
   maxtrt = vname(_trt[i]);
   drop i;
   run;
proc print;
   run;

2017-10-17_8-14-25.png

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
  • 1314 views
  • 2 likes
  • 3 in conversation