BookmarkSubscribeRSS Feed
sas_not_a_user
Calcite | Level 5

I'm not as SAS user, however I'm having to read some SAS code to get some understanding. I was looking into how arrays work and saw this in code but wasn't sure if there is any difference between 0 and 0.

Seems like med{12} is initialised with values from  mth_01 - mth_12, and I was assuming somewhere the values are getting appened/updated. BUt in the loop i see med{i} = 0.; , i see 0. Does it mean all 12 variables are having value as 0. If yes what is difference between 0 and 0.? Or 0. works in loop and =0 without loop are same? 

Code Sample: 

data tmp (drop=med_allowed diff i);
set tmp1;
by individual_id;

 

retain mth_01 - mth_12 medal1;
array med{12} mth_01 - mth_12;

 

if first.individual_id then do;
do i=1 to 12;
med{i} = 0.;
end;
medal1 = 0;
end;

if last.individual_id then output;
run;

3 REPLIES 3
ballardw
Super User

SAS will not treat 0 and 0. (with a decimal) differently as numeric values. Just inconsistent programming by the author. Perhaps the programmer modified some existing code that used a value like 0.5 and just deleted the 5.

 

You should post code or log results using the code boxes opened with either the </> or "running man" icons to prevent the main windows from reformatting your text/ code.

If the source if formatted such as:

data tmp (drop=med_allowed diff i);
   set tmp1;
   by individual_id;
   retain mth_01 - mth_12 medal1;
   array med{12} mth_01 - mth_12;
   if first.individual_id then do;
      do i=1 to 12;
         med{i} = 0.;
      end;
      medal1 = 0;
   end;
if last.individual_id then output;
run;

the code will be easier to read and follow the flow (at least if indented consistently and carefully).

sas_not_a_user
Calcite | Level 5
Thank you @ballardw . Will follow code formatting going forward. To ask a bit more here:
1. isn't array med of size 12 being initialised with mth_01 to mth_12. And later one med{i} which is mth_01 to mth_12 being initialized with value 0. I'm confused because I can see in sample report out of the whole code, columns are being derived. So was confused with 0 intialization.

2. A statement like : (this piece of code is right above if last.individual_id line)
<
if (med_allowed ne 0) then med{diff+1} = med_allowed;
medal1 = sum(medal1,med_allowed);
/>
to me it looks like med{diff+1} is initialised with med_allowed value, but doesn't seem being used.
I can't understand if its author's way of code or its SAS way of doing.
If I'm right, can you please confirm If i can read it as
<
if (med_allowed ne 0) ;
medal1 = sum(medal1,med_allowed);
/>
P.S: This is very old code and can't reach to author. Thus asking for help here. thank you. I have used SAS documentation to understand concepts overallan and they have been helpful.
Tom
Super User Tom
Super User

@sas_not_a_user wrote:
Thank you @ballardw . Will follow code formatting going forward. To ask a bit more here:
1. isn't array med of size 12 being initialised with mth_01 to mth_12. And later one med{i} which is mth_01 to mth_12 being initialized with value 0. I'm confused because I can see in sample report out of the whole code, columns are being derived. So was confused with 0 intialization.

An ARRAY statement just tells the data step compiler which variables are meant when you use the array name in the code.  So MED[4] is just another way to refer to MTH_04.  It would only initialize those variables if you included the optional list of initial values after the list of variable names in the ARRAY statement.

 

However the assignment statement inside of the DO I=1 to 12 loop is where values are being to variables MTH_01 to MTH_12.  Because this called when FIRST.INDIVIDUAL_ID is TRUE then it is done when you reach the first observation for that value of INDIVIDUAL_ID.  You could call that DO loop the place where the values are initialized.

 

2. A statement like : (this piece of code is right above if last.individual_id line)
<
if (med_allowed ne 0) then med{diff+1} = med_allowed;
medal1 = sum(medal1,med_allowed);
/>
to me it looks like med{diff+1} is initialised with med_allowed value, but doesn't seem being used.
I can't understand if its author's way of code or its SAS way of doing.
If I'm right, can you please confirm If i can read it as
<
if (med_allowed ne 0) ;
medal1 = sum(medal1,med_allowed);
/>
P.S: This is very old code and can't reach to author. Thus asking for help here. thank you. I have used SAS documentation to understand concepts overallan and they have been helpful.

There is a big difference between an IF THEN statement and a subsetting IF statement.  In an IF THEN statement if the condition is false only  the statement after the THEN is skipped.  With an subsetting IF statement if the condition is false the current iteration of the data step ends immediately and the data step moves directly to the next iteration, skipping any other statements (including any implied OUTPUT statement that is added to the end if the data step has no explicit OUTPUT statements).

 

We can't really tell if your substitution will end up doing the same thing without seeing the full data step.  (And even then depending on the data they might end up doing the same thing, even if for a different input data they wouldn't).

SAS INNOVATE 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

Register now!

5 Steps to Your First Analytics Project Using SAS

For SAS newbies, this video is a great way to get started. James Harroun walks through the process using SAS Studio for SAS OnDemand for Academics, but the same steps apply to any analytics project.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 793 views
  • 4 likes
  • 3 in conversation