BookmarkSubscribeRSS Feed
Jeg123
Calcite | Level 5

I am trying to replace values using a script and for the first part I dont expect any changes, but SAS changes the values 'NaN' to '', which I dont understand why. Short example below

 

data test;
infile datalines delimiter=',';
	input var $ row date $ group $;
	datalines;
NaN,1,31OCT2016,-1-1d
NaN,2,31OCT2016,-1-1d
GCXK,3,30APR2017,-1-1d
;

DATA test2;
	SET test;
	BY group;
	RETAIN new_var;
	
	IF var NE 'NaN' then new_var = var;
	IF var EQ 'NaN' then var = new_var;

	IF last.var then new_var = 'NaN';

	DROP new_var;
RUN;
1 REPLY 1
Kurt_Bremser
Super User

@Jeg123 wrote:

I am trying to replace values using a script and for the first part I dont expect any changes, but SAS changes the values 'NaN' to '', which I dont understand why. Short example below

 

data test;
infile datalines delimiter=',';
	input var $ row date $ group $;
	datalines;
NaN,1,31OCT2016,-1-1d
NaN,2,31OCT2016,-1-1d
GCXK,3,30APR2017,-1-1d
;

DATA test2;
	SET test;
	BY group;
	RETAIN new_var;
	
	IF var NE 'NaN' then new_var = var;
	IF var EQ 'NaN' then var = new_var;

	IF last.var then new_var = 'NaN';

	DROP new_var;
RUN;

Your code contains a mistake.

Maxim 2: read the log:

36         DATA test2;
37         	SET test;
38         	BY group;
39         	RETAIN new_var;
40         	
41         	IF var NE 'NaN' then new_var = var;
42         	IF var EQ 'NaN' then var = new_var;
43         
44         	IF last.var then new_var = 'NaN';
45         
46         	DROP new_var;
47         RUN;

NOTE: Variable 'last.var'n is uninitialized.
NOTE: There were 3 observations read from the data set WORK.TEST.
NOTE: The data set WORK.TEST2 has 3 observations and 4 variables.
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.00 seconds

Just to get you on the right path: ask yourself: "from where does the first NOTE come from?"

 

About your question:

In obs #1, the first condition is FALSE, so new_var is not set.

The second condition is TRUE, so var is set to new_var (which is still missing, see above)

In obs #2, the same happens.

In obs #3, the first condition is TRUE, so new_var is set.

The second condition is FALSE, so var is not changed.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 1 reply
  • 831 views
  • 0 likes
  • 2 in conversation