BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
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
Tom
Super User Tom
Super User

Your code looks very close.

Fix your DATA statement. Right now you are creating two datasets, one named BY and one named THREE, that both have the same data.

You forgot to assign any values to your new variables.

Can you figure out what statement you would need to set the new variable to three times the old variable?  And where you would put that code?

Kurt_Bremser
Super User

@Flexluthorella wrote:

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?


You don't get any output because of several syntax errors:

data by three:

A statement needs to be terminated with a semicolon, not a colon. And do you really want to create two new datasets?

 

if oldvar{i} = 999 the call missing (newvar{i});

should be

if oldvar{i} = 999 then call missing (newvar{i});

See this code:

data have;
input x y z a1 a2 a3 a4 a5;
datalines;
1 2 3 4 999 6 7 8
;

data by_three;
set have;
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} ne 999 then newvar{i} = 1;
end;
drop i;
run;

Since all new variables are set to missing when an iteration of the data step starts, call missing() will not have any visible effect, so I did the opposite to show how the code works. Read the log, and you'll see that it's perfectly clean.

KachiM
Rhodochrosite | Level 12

@Flexluthorella 

 

Does this help you?

 

data have;
input x y z a1 a2 a3 a4 a5;
datalines;
1 2 3 4 999 6 7 8
1 3 2 3 9   1 2 3
4 5 1 7 1   2 1 2
;
run;

data want;
set have;
array oldvar  x y z a1-a5;
array newvar  nx ny nz na1-na5;
do i = 1 to dim(oldvar);
   if oldvar[i] = 999 then newvar[i] = .;
   else newvar[i] = oldvar[i] * 3;
end;
drop i;
run;

The output:

Capture_01.JPG

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

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
  • 4217 views
  • 0 likes
  • 7 in conversation