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

Hi Community;

 

I need a solution to select the condition on the first character variable then updates the value of the other character variables before creating the final character variable. For example.

 

DATA have;
input a $ b $ c $;
datalines;
0 2 7
1 5 2
1 7 3
0 3 1
;
run;
 
The request is if a is 1 then update the value in b and c as 
when  a="1" and then when b or c=1  then the new value of  b or c=0.07;
when  a="1" and then when b or c=2 then the new value of  b or c=0.14;
when  a="1" and then when b or c=3  then the new value of  b or c=0.36;
when  a="1" and then when b or c=4  then the new value of  b or c=0.64;
when  a="1" and then when b or c=5  then the new value of  b or c=1;
when  a="1" and then when b or c=6  then the new value of  b or c=3;
when  a="1" and then when b or c=7  then the new value of  b or c=5;
 
The final step is to create a new character variable d as the sum of b and c. 
 
Therefore the wanted table will be like
 
a      b      c       d
0      2      7        .
1      1    0.14   1.14
1     5     0.36   5.36
0      3      1       .
 
All the variables are character variables.
 
Thank you for your help. 
 
1 ACCEPTED SOLUTION

Accepted Solutions
CHL0320
Obsidian | Level 7

Thank you and I am able to find what is wrong in my previous code. Thank you so much.

DATA have;
input a $ b $ c $;
datalines;
0 2 7
1 5 2
1 7 3
0 3 1
;
run;

 

data want;
set have;

if a="1" then
DO;
select(b);
when ("1") b="0.07";
when ("2") b="0.14";
when ("3") b="0.36";
when ("4") b="0.64";
when ("5") b="1";
when ("6") b="3";
when ("7") b="5";
otherwise;
end;

select(c);
when ("1") c="0.07";
when ("2") c="0.14";
when ("3") c="0.36";
when ("4") c="0.64";
when ("5") c="1";
when ("6") c="3";
when ("7") c="5";
otherwise;
end;

e=b+c;
d=put(e,4.2);
drop e;
END;
run;

View solution in original post

2 REPLIES 2
ScottBass
Rhodochrosite | Level 12

This doesn't look particularly difficult.  Can you post the code you've attempted, and describe what didn't work?

 

Read the doc on IF/THEN/ELSE and SELECT/WHEN/OTHERWISE.


Please post your question as a self-contained data step in the form of "have" (source) and "want" (desired results).
I won't contribute to your post if I can't cut-and-paste your syntactically correct code into SAS.
CHL0320
Obsidian | Level 7

Thank you and I am able to find what is wrong in my previous code. Thank you so much.

DATA have;
input a $ b $ c $;
datalines;
0 2 7
1 5 2
1 7 3
0 3 1
;
run;

 

data want;
set have;

if a="1" then
DO;
select(b);
when ("1") b="0.07";
when ("2") b="0.14";
when ("3") b="0.36";
when ("4") b="0.64";
when ("5") b="1";
when ("6") b="3";
when ("7") b="5";
otherwise;
end;

select(c);
when ("1") c="0.07";
when ("2") c="0.14";
when ("3") c="0.36";
when ("4") c="0.64";
when ("5") c="1";
when ("6") c="3";
when ("7") c="5";
otherwise;
end;

e=b+c;
d=put(e,4.2);
drop e;
END;
run;

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