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

Hello experts:

I would like to do the following steps.  

 

  1. The dataset has the variables list below:
    1. Error
    2. Dose1_Group1
    3. Date1_Group1
    4. Productor1_Group1
    5. Manufacture1_Group1
    6. Dose2_Group2
    7. Date2_Group2
    8. Productor2_Group2
    9. Manufacture2_Group2
  2. When variable 'Error' =1, I would like to switch the group 1 variables which are 'Dose1,' 'Date1,' 'Productor1,' 'Manufacture1.' with the group 2 variable 'Dose2,' 'Date2,' 'Productor2,' 'Manufacture2.'
  3. When variable 'Error' =0, I DO NOT want to switch the group 1 variables which are 'Dose1,' 'Date1,' 'Productor1,' 'Manufacture1.' with the group 2 variable 'Dose2,' 'Date2,' 'Productor2,' 'Manufacture2.'

I am wondering if the SAS could perform this procedure. Thanks ahead.

1 ACCEPTED SOLUTION

Accepted Solutions
Jagadishkatam
Amethyst | Level 16

Then you can try the below code so as to reduce the number of the lines

 

data want;
set have;
array grp1(*) Dose1_Group1 Date1_Group1 Productor1_Group1 ;
array grp2(*) Dose2_Group2 Date2_Group2 Productor2_Group2 ;
array grp3(*) temp1 temp2 temp3 ;
do i = 1 to dim(grp1);
grp3(i)=grp1(i);
if error=1 then do;
grp1(i)=grp2(i);
grp2(i)=grp3(i);
end;
end;
if error=1 then do;
temp4=Manufacture1_Group1;
Manufacture1_Group1=Manufacture2_Group2;
Manufacture2_Group2=temp4;
end;
run;

 

 

Thanks,
Jag

View solution in original post

5 REPLIES 5
PaigeMiller
Diamond | Level 26
data want;
    set have;
    if error=1 then do;
        temp=dose1_group1;
        dose1_group1=dose2_group2;
        dose2_group2=temp;
        /* Repeat the above for all other variables */
        /* which could be done using ARRAYs if desired */
    end;
    drop temp;
run;

 

By the way, why make your variable names so complicated? Wouldn't dose1 and dose2 be sufficient, instead of dose1_group1 and dose2_group2?

--
Paige Miller
Jagadishkatam
Amethyst | Level 16

If you consider that except error variable, all the other variables are character then you can use arrays as below

 

data want;
set have;
array grp1(*) $ Dose1_Group1 Date1_Group1 Productor1_Group1 Manufacture1_Group1;
array grp2(*) $ Dose2_Group2 Date2_Group2 Productor2_Group2 Manufacture2_Group2;
array grp3(*) $ temp1 temp2 temp3 temp4;
do i = 1 to dim(grp1);
grp3(i)=grp1(i);
if error=1 then do;
grp1(i)=grp2(i);
grp2(i)=grp3(i);
end;
end;
run;
Thanks,
Jag
ybz12003
Rhodochrosite | Level 12

I apologize I didn't specify the variables.  Some variables like dose, date, and product are numeric, some variable like manufacture is character.  

Jagadishkatam
Amethyst | Level 16

Then you can try the below code so as to reduce the number of the lines

 

data want;
set have;
array grp1(*) Dose1_Group1 Date1_Group1 Productor1_Group1 ;
array grp2(*) Dose2_Group2 Date2_Group2 Productor2_Group2 ;
array grp3(*) temp1 temp2 temp3 ;
do i = 1 to dim(grp1);
grp3(i)=grp1(i);
if error=1 then do;
grp1(i)=grp2(i);
grp2(i)=grp3(i);
end;
end;
if error=1 then do;
temp4=Manufacture1_Group1;
Manufacture1_Group1=Manufacture2_Group2;
Manufacture2_Group2=temp4;
end;
run;

 

 

Thanks,
Jag
ybz12003
Rhodochrosite | Level 12

Hello, 

 

Most of your codes are correct, except adding one "$" to the character switch.  It's because I have multiple character variables.   

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 5 replies
  • 855 views
  • 0 likes
  • 3 in conversation