DATA Step, Macro, Functions and more

IF statement in DATA STEP gets executed even when the condition is false

Reply
Contributor
Posts: 27

IF statement in DATA STEP gets executed even when the condition is false

 

I am trying to convert a numeric variable to character , In case if it contains any numeric values . Strangely the IF statement gets executed even when the condition is not true and creates a blanl column .

 

Code :

 

DATA SASUSER.CLIENT_DATA;
SET sasuser.Raw_data;
ID_NUM = put(input(trim(ID_Code),best12.), z11. );
if index ne _N_ then delete;
if indicator_value in (1,2) THEN DO;
value =put(indicator_value,1.);
end;
RUN;

 

LOG :

 

22 GOPTIONS ACCESSIBLE;
23 DATA SASUSER.CLIENT_DATA;
24 SET sasuser.Raw_data;
25 ID_NUM = put(input(trim(ID_Code),best12.), z11. );
26 if index ne _N_ then delete;
27 if indicator_value in (1,2) THEN DO;
28 value =put(indicator_value,1.);
29 end;
30 RUN;
NOTE: Numeric values have been converted to character values at the places given by: (Line)Smiley SadColumn).
25:26
NOTE: Character values have been converted to numeric values at the places given by: (Line)Smiley SadColumn).
27:5
NOTE: Invalid numeric data, indicator_value='Y' , at line 27 column 5.
index=1 ID_Code=603459315 Client_Member=308312227
indicator_value=Y ID_NUM=00603459315 value=
_ERROR_=1 _N_=1

 

The new variable needs to be created only when indicator_value is 1 or 2 , but the highlighted note above shows that the IF block was executed and I can see a blank variable value in dataset.

 

 

Super User
Posts: 10,500

Re: IF statement in DATA STEP gets executed even when the condition is false

Variables do not exist conditionally in SAS datasets. The variable is either there or not. If no value is assigned then SAS will have a missing value.

Super User
Posts: 17,824

Re: IF statement in DATA STEP gets executed even when the condition is false

That's the correct behaviour. Even the if condition is going to have issues since its assuming the variables are numeric based on your code but it appears to be character, which is why you see the note about conversion. 

As long as a variable is referenced in a data step it's created. 

 

 

If you want to conditionally create a variable based on type you'll need macro logic. 

 

It may also be worth explaining what you're trying to do overall and we can make alternative suggestions. 

Contributor
Posts: 27

Re: IF statement in DATA STEP gets executed even when the condition is false


Thanks for the explanation . 

The raw data file i  get contain  indicator_value either  Y,N or 1,2 (Character or Numeric) . I need to convert all to common data type so that i can do calculations by grouping records on  these categories  , create new variables by using this variable in case statement  and create a custom character format ( '1' = 'Y'  '2' ='N')  to use in proc report . 

My Case statements and proc report , which are written for character variable data , fails when  the program encounters numeric variable value .

 

I can not use both numeric and characater data in case statement (  IN ('Y','N','1','2',1,2)  ) ,  and can not have single format created for both charcater and numeric data  to use in proc report.

 

For Now , I used vtype that fits my requirement , but any other suggestions are appreciated.

 

DATA SASUSER.CLIENT_DATA;
SET sasuser.Raw_data;
ID_NUM = put(input(trim(ID_Code),best12.), z11. );
if index ne _N_ then delete;
if vtype(indicator_value) ='N' THEN
value =put(indicator_value,1.);
else value=indicator_value;
RUN;

 

Thanks,

MG

 

 

 

 

 

 

 

Super User
Posts: 17,824

Re: IF statement in DATA STEP gets executed even when the condition is false

Are you dealing with multiple files?  Otherwise a variable only holds one data type. 

I think you need to provide more information. 

Contributor
Posts: 27

Re: IF statement in DATA STEP gets executed even when the condition is false

yes , multiple files . 

Super User
Posts: 17,824

Re: IF statement in DATA STEP gets executed even when the condition is false

If they're text, not Excel, I would recommend fixing as many issues in import process rather than after the fact. 

Super User
Posts: 10,500

Re: IF statement in DATA STEP gets executed even when the condition is false

A custom informat could fix the issue when reading a file where the values vary from file to file but mean the same thing. BUT to assign an informat you will need to read it with a datastep. If you have been reading text files using proc import the log will have the code that you can copy and modify (and take some time to standardize text lengths and such).

 

A format that looks like:

 

proc format;

invalue $MyYesNo

'1','Y' = 'Y'

'2','N' = 'N'

;

run;

 

The datastep would include

informat indicator_value $MyYesNo. ;

before the input statement.

Again this would be modifying code generated by Proc Import (assuming these files are the same except for this behavior).

Fix a consistency problem as early as practical and it simiplifies stuff down stream.

 

BTW, I would read them into numeric and with values 1 for Yes and 0 for no as there are so many things that work better.

Some examples: Sum of such a coded variable will tell you the number of Yes, mean is percentage, code like "If variable then" works because the 1 is considered true and 0 false.

 

Contributor
Posts: 27

Re: IF statement in DATA STEP gets executed even when the condition is false

Thank you..

Ask a Question
Discussion stats
  • 8 replies
  • 371 views
  • 3 likes
  • 3 in conversation