Hi, I would like to replace values in C and D with value from B. Can anyone help to create code using array or macro ? Thank you so much !
data have;
input A $ B C $ D ;
datalines;
CA 1 1 1
CA 2 B 1
CA 3 B 9
CA 4 B 8
CA 6 R 2
NV 1 B 1
NV 2 B 1
NV 3 B 1
NV 4 B 1
NV 5 B 1
NV 6 B 1
NV 1 R 4
NV 2 R 6
;
run;
Neither macro nor arrays are needed.
data want;
set have;
c = put(b,best.);
d = b;
run;
@octrout wrote:
Thanks for the reply. What if i have many variables to replace values ?
Provide a more complete example. It is likely to be a variation on what @Kurt_Bremser has already provided.
Macro is likely not the way to go though a number of arrays may be needed depending on your rules for which values are used to assign to other variables.
Hi,
I have about 60 variables that need to replace with values from one variable.
This would be an example data set need to replace c,d,e,f,g with values from B. Thanks !
data have;
input A $ B C $ D E F G ;
datalines;
CA 1 1 1 20 7 10
CA 2 B 1 2 5 18
CA 3 B 9 3 5 18
CA 4 B 8 3 3 7
CA 6 R 2 4 2 6
NV 1 B 1 4 1 9
NV 2 B 1 4 1 10
NV 3 B 1 5 7 18
NV 4 B 1 5 4 8
NV 5 B 1 5 4 7
NV 6 B 1 5 6 8
NV 1 R 4 5 7 9
NV 2 R 6 5 8 10
;
run;
@octrout wrote:
Hi,
I have about 60 variables that need to replace with values from one variable.
This would be an example data set need to replace c,d,e,f,g with values from B. Thanks !
data have; input A $ B C $ D E F G ; datalines; CA 1 1 1 20 7 10 CA 2 B 1 2 5 18 CA 3 B 9 3 5 18 CA 4 B 8 3 3 7 CA 6 R 2 4 2 6 NV 1 B 1 4 1 9 NV 2 B 1 4 1 10 NV 3 B 1 5 7 18 NV 4 B 1 5 4 8 NV 5 B 1 5 4 7 NV 6 B 1 5 6 8 NV 1 R 4 5 7 9 NV 2 R 6 5 8 10 ; run;
Declare one array for character and another for numeric variables. Then use my assignments in do loops for 1 to dim(array), one for the numeric and one for the character array.
Then you need to clarify your logic. You can create an array to loop through multiple variables if needed but given what you've posted there's no need for such a solution.
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.
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.