How do you create a temp dataset multiplying all numeric variables (I coded as oldvar{*} since I do not know them all) by 3 from an original dataset into a new dataset without overwriting the original numbers? Like its similar to the following code but I dont want them replaced in the orginal dataset, I want them in a new data set called newvar. See below for incorrect code:
do i = 1 - dim(oldvar);
if oldvar(i) then 999 then call missing (newvar{i});
end;
drop i;
run;
libname learn "XXX";
data by_three;
set learn.Nines;
array oldvar(8) x y z a1 a2 a3 a4 a5;
array newvar(8) x3 y3 z3 a13 a23 a33 a43 a53;
do i = 1 to 8;
newvar{i}=3*oldvar{i};
end;
drop i;
run;
Just for my own understanding, if your data looks like this
data have;
input oldvar1-oldvar3 (c1-c3)(:$);
datalines;
1 2 3 a b c
;
You want the resulting data to look like this, correct?
data want;
input oldvar1-oldvar3 newvar1-newvar3 (c1-c3)(:$);
datalines;
1 2 3 3 6 9 a b c
;
The question is "using dataset Nines, create a new temporary dataset which contains revised variables for all of the numeric variables such that the value of the revised variables is 3 times the original value except when the value is 999- code these as missing. Do this using 2 arrays and do not overwrite the data in the original variables."
@Flexluthorella wrote:
I dont want them replaced in the orginal dataset, I want them in a new data set called newvar.
Do you mean you want a new VARIABLE called newvar? Your title says you want a new variable, your text says you want a new data set. Please clarify.
its a new variable in a new temporary dataset.
if oldvar(i) then 999 then call missing (newvar{i});
If you clean up the syntax here, it seems that your code will do exactly what you want.
Here is the original dataset
here is the code and error i have
The syntax of
array newvar{*} _temporary_;
is not correct. You have to actually name the variables that will appear in the array newvar;
In the future, please include code and log directly in the message (as text, not as a screen capture) by copying the text and then pasting it into the window that appears when you click on the {i} icon (for the log) or the window that appears when you click on the running man icon (for code). Providing code and log as an attachment is not preferred.
Start by learning the very basics of data step programming.
do i = 1 - 5;
is incorrect, as you define a starting value of -4, and no end value.
Iterative do loops need the keyword "to", like
do i = 1 to 5;
You also define array newvar, but use newvars further down the code. That's the cause for the "undefined array reference" error.
But, finally:
Do NOT post code, logs or data in pictures. NEVER EVER.
Copy/paste code into a window opened with the "little running man", and logs and textual data into a window opened with the {i} button.
Assuming that we have nothing better to do than typing data tediously from a picture is not very polite.
I am so sorry I am very new at this and I feel as though the output I need does not make sense. I am still not getting the correct output.
It still include the character variables and the newvar do not show up. How do I add the multiplier of three to the numeric variables in the new dataset bythree?
libname learn "XXX"; data by three: set learn.Nines; array oldvar(8) x y z a1 a2 a3 a4 a5; array newvar(8) x3 y3 z3 a13 a23 a33 a43 a53; do i = 1 to 8; if oldvar{i} = 999 the call missing (newvar{i}); end; drop i; run;
of three to the new variables?
@Flexluthorella wrote:
It still include the character variables and the newvar do not show up. How do I add the multiplier of three to the numeric variables in the new dataset bythree?
libname learn "XXX"; data by three: set learn.Nines; array oldvar(8) x y z a1 a2 a3 a4 a5; array newvar(8) x3 y3 z3 a13 a23 a33 a43 a53; do i = 1 to 8; if oldvar{i} = 999 the call missing (newvar{i}); end; drop i; run;of three to the new variables?
Explain this. What multiplier of three? Where do you want to multiply by three? Give an example. There is no such thing in your code.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.