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

**Edit:** Hopefully this will clear things up - I'm attaching the code:

data test1;
input visit $16.;
datalines;
First
Second
;

 

data nulll;
input index $;
datalines;
.
;

 

data test2;
stop;
set nulll;
run;

 

data test;
set test2 test1;
if index(VISIT, 'Fir')^=0 then do;
index='100';
end;

proc print data=test;
run;

 

Alternatively, the code works properly if phrased as 

data test1;
input visit $16.;
datalines;
First
Second
;

 

data nulll;
input index $;
datalines;
.
;

 

data test2;
stop;
set nulll;
run;

 

data test;
set test2 test1;
run;

 

data test;
set test;
if index(VISIT, 'Fir')^=0 then do;
index='100';
end;

 

proc print data=test;
run;

 

Hello everyone,

I stumbled across an odd phenomenon and was wondering whether a more experienced user could shed some light on the topic.

Suppose I want to assign '1' (as a character) to an indicator variable if a condition is met and I want it to be missing otherwise. I also want to have the names of the variables fixed for which I created a dummy empty dataset (called emptydata) with the appropriate names of the variables that will be generated. The code itself looks like this:

 

data work_data;

set empty_data prep_data;

if (condition) then indicator='1';

 

where prep_data is a dataset containing data we already have, empty_data is there just for the naming and indicator is a variable NOT present in any of the datasets.

There, however, seems to be a problem - the indicator trivially becomes '1' for all observations regardless of the condition. If, on the other hand, I used the following code:

 

data work_data;

set empty_data prep_data;

 

data work_data;

set work_data;

if (condition) then indicator='1';

 

then things seem to be normal, that is, if I split it into 2 data steps (I might be wrong though). Has anyone experience something like that? Any help will be appreciated.

 

All the best,

teli4

1 ACCEPTED SOLUTION

Accepted Solutions
SASJedi
SAS Super FREQ

This issue is a result of reading in the INDEX variable from a SAS data set. Variables read from a SAS data set are not re-initialized automatically when the step reiterates.  The code doesn't re-initialize the INDEX variable either, so once set to '100' by your program, the value persists in the PDV. Adding logic to set the value to missing will resolve the issue:

data test;
   set test2 test1 ;
   if index(VISIT, 'Fir')^=0 then do;
      index='100';
   end;
   else call missing(index);
run;

proc print data=test;
run;

Result:

Obs index visit
1 100 First
2   Second

May the SAS be with you!

Mark

Check out my Jedi SAS Tricks for SAS Users

View solution in original post

5 REPLIES 5
PaigeMiller
Diamond | Level 26

We would need to see a portion of these data sets to be able to tell you what is going on. Please follow these instructions to create SAS code that contains a small portion of these data sets for us to use. Do not provide the data in attachments or screen captures.

--
Paige Miller
ballardw
Super User

It is relatively easy to make mistakes in logic, especially for new programmers regarding logical assignments. So you really need to share the exact complete code of your "condition".

 

We also see people that use code from other programming languages that behaves differently in SAS. So just because it works one place doesn't mean that it will work with SAS.

 

And why a character value????

SASJedi
SAS Super FREQ

This issue is a result of reading in the INDEX variable from a SAS data set. Variables read from a SAS data set are not re-initialized automatically when the step reiterates.  The code doesn't re-initialize the INDEX variable either, so once set to '100' by your program, the value persists in the PDV. Adding logic to set the value to missing will resolve the issue:

data test;
   set test2 test1 ;
   if index(VISIT, 'Fir')^=0 then do;
      index='100';
   end;
   else call missing(index);
run;

proc print data=test;
run;

Result:

Obs index visit
1 100 First
2   Second

May the SAS be with you!

Mark

Check out my Jedi SAS Tricks for SAS Users
teli4
Fluorite | Level 6

Thank You very much for the detailed answer - much appreciated! However, I'm still confused as to why first setting AND then using the IF operator in a second data step works fine while when setting and using IF in the same step gives the wrong result? I'm assuming it's something to do with the initialisation of the variable "index".

 

Also, if none of the conditions are met, I thought that it will just evaluate "index" as missing as it simply doesn't have anything assigned  - clearly, however, in some cases it sets it to missing (when 2 data steps are used) and sometimes it "carries over" the previous value (when 1 data step is used).

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!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 5 replies
  • 865 views
  • 1 like
  • 5 in conversation