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

Hello how to use conditions on two columns with select when statements:

I am not getting any errors, but my results are not correct. I want to set the new variable x based on the col1 and col2 conditions using select when only. Please find my code below.

data test;

input col1 $ col2;

length x $10;

datalines;

A 1

A 1

A 2

A 3

B 1

B 2

B 3

C 1

C 1

;;

run;

data test2;

set test;

select (col1);

  when (A) do;

     if col2 = 1 then do;

     x= 'test_A1';

     end;

  when (B) do;

      if col2 = 3; then do;

     x = 'test_B3 ';

       end;

otherwise put 'other';

end;

end;

Thanks.


1 ACCEPTED SOLUTION

Accepted Solutions
Peter_C
Rhodochrosite | Level 12


something like

select ;

  when (col1='A' and col2 = 1 ) do;

          x= 'test_A1';

     end;

  when (col1='B' and col2 = 3 ) do;

          x = 'test_B3 ';

       end;

otherwise put 'other';

end;

View solution in original post

2 REPLIES 2
Peter_C
Rhodochrosite | Level 12


something like

select ;

  when (col1='A' and col2 = 1 ) do;

          x= 'test_A1';

     end;

  when (col1='B' and col2 = 3 ) do;

          x = 'test_B3 ';

       end;

otherwise put 'other';

end;

sas121987
Calcite | Level 5

Thanks Peter, It worked.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 2 replies
  • 749 views
  • 0 likes
  • 2 in conversation