BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Aayushi_17
Quartz | Level 8

Hii

 

I am practicing SAS with dummy datasets i came across this condition where a variables contains both numeric and character .

Data 1Data 2
present 4
abs6
129
332.
21.
31.
274
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

 

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

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;

View solution in original post

14 REPLIES 14
Aayushi_17
Quartz | Level 8
for vartype we have to use array function....cant we find it out without using array
PeterClemmensen
Tourmaline | Level 20

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?

Aayushi_17
Quartz | Level 8
if the type of the variable is numeric then i have apply those condition
PeterClemmensen
Tourmaline | Level 20

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.

PeterClemmensen
Tourmaline | Level 20

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;
PeterClemmensen
Tourmaline | Level 20

@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.

Aayushi_17
Quartz | Level 8

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

Ksharp
Super User

Check VTYPE() function.

data class;
 set sashelp.class;
 t1=vtype(name);
 t2=vtype(age);
run;
Aayushi_17
Quartz | Level 8
what if the number is also stored in character format vtype returns as C even for that
Ksharp
Super User

Then Check NOTDIGIT() ANYDIGIT()+COMPRESS().

 

 

Ksharp
Super User

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;
RW9
Diamond | Level 26 RW9
Diamond | Level 26
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.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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
  • 14 replies
  • 49513 views
  • 9 likes
  • 4 in conversation