BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Nidhi_vats
Calcite | Level 5

Hi I have data like this 

 

var1var2var3
a1610
a619
a89
a713
b1414
b1011
a1416
b1612
c1312
b1011
c718
a117
b137
c1410
c1513
b1715
c1411

 

and want to apply these 3 conditions

1:    

       var2<10 

       var3>15

then the value in new final column should be = "a-1" (if var1=a), "1-b"(if var1=b), "c-1"(if var1=c)

2:   

       var2>10

       var3>15

 then the value in new final column should be = "a-2" (if var1=a), "2-b"(if var1=b), "c-2"(if var1=c)

3:

      var2>10

      var3<15

then the value in new final column should be = "a-3" (if var1=a), "3-b"(if var1=b), "c-3"(if var1=c)

 

I want my output be like this 

 

var1var2var3final
a1610a-3
a619a-1
a89 
a713 
b14143-b
b1011 
a1416a-2
b16123-b
c1312c-3
b1011 
c718c-1
a117a-3
b1373-b
c1410c-3
c1513c-3
b1715 
c1411

c-3

 

Please help me in code

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

@Nidhi_vats wrote:

I also tried with these tree loops but is it possible that we can apply these three conditions in single and compact loop?


Hello @Nidhi_vats,

 

Is this "compact" enough?

data want;
set have;
length final $3;
_n_=3*(var2>10)*(var3~=15)+(var3>15)*((var2~=10)-2*(var2>10));
if _n_ then final=putc(cats(var1,'-',_n_),ifc(var1='b','$revers','$'),3);
run;

 

View solution in original post

7 REPLIES 7
pink_poodle
Barite | Level 11
data want;
set have;
if var2 lt 10 and var3 gt 15
then do;
if var1 = 'a' then final = 'a-1';
else if var1 = 'b' then final = 'b-1';
else if var1 = 'c' then final = 'c-1';
end;

... similarly for points 2 and 3 ...

run;
Nidhi_vats
Calcite | Level 5

Please help me with a single loop

ballardw
Super User

Here's one way.

data want;
   set have;
   length newfinal $ 5;
   select ;
      when (var2<10) select ;
         when (var3>15) select (var1);
            when ('a','c') newfinal = catx('-',var1,1);
            when ('b')     newfinal = catx('-',1,var1);
            otherwise;
         end;
         otherwise;
      end;
      when (var2>10) select ;
         when(var3>15) select (var1);
            when ('a','c') newfinal = catx('-',var1,2);
            when ('b')     newfinal = catx('-',2,var1);
            otherwise;
         end;
         when(var3<15) select (var1);
            when ('a','c') newfinal = catx('-',var1,3);
            when ('b')     newfinal = catx('-',3,var1);
            otherwise;
         end;
         otherwise;
      end;
      otherwise;
   end;
run;

 

 

SELECT/WHEN/END blocks are a way of simplifying multiple similar If/then/else. without a variable after select you can place an expression in the WHEN statement to specify condition. With a variable then you can provide lists of values.

 

The otherwise statement in each block is what you would do if none of the specified conditions were true.

CATX is a string function to place the first value between all occurrences of other variables and removes any trailing blanks there might be.

 

Another using a function that can return up to three values depending on a condition

data want;
   set have;
   length newfinal $ 5;
   select ;
      when (var2<10) select ;
         when (var3>15) newfinal= ifc(var1='b','1-b',catx('-',var1,1));
         otherwise;
      end;
      when (var2>10) select ;
         when (var3>15) newfinal= ifc(var1='b','2-b',catx('-',var1,2));
         when (var3<15) newfinal= ifc(var1='b','3-b',catx('-',var1,3));
         otherwise;
      end;
      otherwise;
   end;
run;
Nidhi_vats
Calcite | Level 5

I also tried with these tree loops but is it possible that we can apply these three conditions in single and compact loop?

andreas_lds
Jade | Level 19

@Nidhi_vats wrote:

I also tried with these tree loops but is it possible that we can apply these three conditions in single and compact loop?


Maybe i am blind, but i can't see any loops (except for the implicit data-step-loop) in the suggested solution.

FreelanceReinh
Jade | Level 19

@Nidhi_vats wrote:

I also tried with these tree loops but is it possible that we can apply these three conditions in single and compact loop?


Hello @Nidhi_vats,

 

Is this "compact" enough?

data want;
set have;
length final $3;
_n_=3*(var2>10)*(var3~=15)+(var3>15)*((var2~=10)-2*(var2>10));
if _n_ then final=putc(cats(var1,'-',_n_),ifc(var1='b','$revers','$'),3);
run;

 

ballardw
Super User

@FreelanceReinh wrote:

@Nidhi_vats wrote:

I also tried with these tree loops but is it possible that we can apply these three conditions in single and compact loop?


Hello @Nidhi_vats,

 

Is this "compact" enough?

data want;
set have;
length final $3;
_n_=3*(var2>10)*(var3~=15)+(var3>15)*((var2~=10)-2*(var2>10));
if _n_ then final=putc(cats(var1,'-',_n_),ifc(var1='b','$revers','$'),3);
run;

 


Slick.

 

Of course I am waiting for the OP to come back with another question involving var1 = d (or ??) and another operation like * ….

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 1651 views
  • 1 like
  • 5 in conversation