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

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;

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26
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;
--
Paige Miller

View solution in original post

20 REPLIES 20
PeterClemmensen
Tourmaline | Level 20

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
;
Flexluthorella
Obsidian | Level 7

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."

 

 

PaigeMiller
Diamond | Level 26

@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.

--
Paige Miller
Flexluthorella
Obsidian | Level 7

its a new variable in a new temporary dataset.

PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
Flexluthorella
Obsidian | Level 7
This is not the correct answer; I keep getting error
tomrvincent
Rhodochrosite | Level 12
I doubt you'll ever get a positive response with a post like that. If you're new to SAS, why are you working with arrays? Keep it simple, explain what you're trying to accomplish and remain moderately humble.
PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
Kurt_Bremser
Super User

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.

Flexluthorella
Obsidian | Level 7

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?

PaigeMiller
Diamond | Level 26

@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.

--
Paige Miller
Flexluthorella
Obsidian | Level 7
The values in the variables x y z a1 a2 a3 a4 a5. They are simple numbers ranging from 2-999. Example the 1st value in variable X is 4 therefore X3 in newvar should be 12.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 20 replies
  • 2496 views
  • 0 likes
  • 7 in conversation