Hii
I am practicing SAS with dummy datasets i came across this condition where a variables contains both numeric and character .
Data 1 | Data 2 |
present | 4 |
abs | 6 |
12 | 9 |
332 | . |
21 | . |
31 | . |
27 | 4 |
not defined | 7 |
i got a condition like
(i)if data1 is numeric and data 2 is not missing then data3=data1
(ii) if data 1 is not numeric then data 4 is null
can anyone please help me how to solve this ....thanks in advance
I think this may be a starting point to what you want to do. Here, I use the INPUT function to determine whether Data1 can be meaningfully represented as a number.
Then I apply your logic to the best of my ability.
data have;
length Data1 $20;
input Data1 $ Data2;
infile datalines dlm=',';
datalines;
present,4
abs,6
12,9
332,.
21,.
31,.
27,4
not defined,7
;
options nonotes;
data want(drop=a);
set have;
a=input(Data1, best.);
if (a ne .) and data2 ne . then data3=a;
if a eq . then data4=.;
run;
options notes;
Use the VTYPE Function
Some to think of it, I don't think you are actually interested in the type of the variable. You want to know whether the variable value can be represented as a number, correct?
The type of the Data1 is always character. otherwise the value "present" could not be there. That means that the value "12" is also character here.
I think this may be a starting point to what you want to do. Here, I use the INPUT function to determine whether Data1 can be meaningfully represented as a number.
Then I apply your logic to the best of my ability.
data have;
length Data1 $20;
input Data1 $ Data2;
infile datalines dlm=',';
datalines;
present,4
abs,6
12,9
332,.
21,.
31,.
27,4
not defined,7
;
options nonotes;
data want(drop=a);
set have;
a=input(Data1, best.);
if (a ne .) and data2 ne . then data3=a;
if a eq . then data4=.;
run;
options notes;
@Aayushi_17, let me just emphasize that though I tried to apply your logic, it does not make much sense as data4 will result in a missing value no matter what.
the numeric is stored in character value which i have to convert and after that i will get the number as output in data 4 and the character values like present will return as null in data 4
Check VTYPE() function.
data class;
set sashelp.class;
t1=vtype(name);
t2=vtype(age);
run;
Then Check NOTDIGIT() ANYDIGIT()+COMPRESS().
Or Try the following code.
data _null_;
t1='1234.23 ';
x1=resolve(cats('%datatyp(',t1,')'));
put t1= x1=;
t2='23232 ';
x2=resolve(cats('%datatyp(',t2,')'));
put t2= x2=;
t3='2344wew';
x3=resolve(cats('%datatyp(',t3,')'));
put t3= x3=;
run;
data want; set have; if lengthn(compress(data1,"","d"))=0 and data2 ne . then data3=input(data1,best.); else data4=.; run;
Gotta say, you logic doesn't make much sense, data4 will always be . even if the condition else is true.
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
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.