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.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 1 reply
  • 1053 views
  • 0 likes
  • 2 in conversation