HI All,
Here is my scenario - I am using proc import , which will set the datatype based on its content , hence I have taken a copy of dataset using 'set' and tried to convert the datatype using only if it satisfies my 'if logic', but it seems still goes in to the if condition even though it doesn't satisfy my condition .
Below sample code , please help out how can I fix this .
Note: variable inside if not supposed to be created as it doesn't pass the criteria ..
data A;
infile cards dlm=',' missover;
input name : $32. age $ ;
cards;
hawking ,20
einstein , 40
carl sagan , 30
;
run;
data dummy;
set A;
if vtype(age) = 'N' then do ;
age_1 = input(age,best.) ;
end;
run;
Hi @arunrami
I suggest that you use conditional processing with %if %then %end because the type is not a function at the "row" level but a metadata
proc sql noprint;
select upcase(type) into: type_age trimmed from dictionary.columns
where libname='WORK' and memname = 'A' and name='age';
quit;
%if "&type_age"="NUM" %then %do ;
data dummy;
set A;
age_1 = input(age,best.) ;
run;
%end;
From which type of file are you importing?
And why do you convert to numeric when age is already numeric (vtype() = 'N')?
@arunrami wrote:
HI All,
Here is my scenario - I am using proc import , which will set the datatype based on its content , hence I have taken a copy of dataset using 'set' and tried to convert the datatype using only if it satisfies my 'if logic', but it seems still goes in to the if condition even though it doesn't satisfy my condition .
Reliance on Proc import, especially if reading data sources that are text such as CSV, is a poor practice. If you know you are going be reading same structured data, which is sort of implied by fixing specific named variables, then better is to read them correctly in the first place using a data step. Then you control the value types at the beginning.
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.