DATA Step, Macro, Functions and more

Error while reading special characters

Accepted Solution Solved
Reply
Contributor
Posts: 37
Accepted Solution

Error while reading special characters

Hello All,

 

I have a input record such as 825,563.25

 

How do I read it in SAS as a variable which will only show - 82556325

 

Please could you provide me with the easiest solution ASAP!

 

Thanks,

Sayan

 

 DATA T_DETAIL;                                         
 LENGTH T_DETAIL_COST    :$10.                          
 ;                                                      
 INFILE TDETAIL DSD MISSOVER ;                          
 INPUT @1 T_DETAIL_COST                                 
 TARGET_STRING = TRANSLATE((T_DETAIL_COST,('','ëï'))) ; 
 ;                                                      
  PUT @4 ' T_DETAIL COST=' T_DETAIL_COST;               
  PUT @4 ' T_DETAIL COST=' NEWSTRING;                   

 

I tried this but all in vain


Accepted Solutions
Solution
‎03-17-2017 09:49 AM
Super User
Posts: 6,928

Re: Error while reading special characters

@GPNaveen: bad programming practice:

24         data test;
25         input price comma10.2;
26         price2=compress(price,,'kd');
27         datalines;

NOTE: Numeric values have been converted to character values at the places given by: (Line):(Column).
      26:17   
NOTE: The data set WORK.TEST has 1 observations and 2 variables.
NOTE: DATA statement used (Total process time):
      real time           0.02 seconds
      cpu time            0.00 seconds

Clean programs should never have automatic numeric type conversions.

Instead use

data test;
input price $10.;
price2=compress(price,,'kd');
datalines;
825,563.25
;
run;

or

data test;
input price comma10.2;
price2=compress(put(price,best.),,'kd');
datalines;
825,563.25
;
run;

I would even define proper length for character variables according to the expected (infile documentation!) range.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers

View solution in original post


All Replies
Occasional Contributor
Posts: 6

Re: Error while reading special characters

Hello,

 

This serves the purpose but not sure if this is how you wanted it.

 

Example:

 

data test;
input price comma10.2.;
price2=compress(price,,'kd');
datalines;
825,563.25
;
run;

 

proc print data=test;
run;

 

Thanks

GP

Solution
‎03-17-2017 09:49 AM
Super User
Posts: 6,928

Re: Error while reading special characters

@GPNaveen: bad programming practice:

24         data test;
25         input price comma10.2;
26         price2=compress(price,,'kd');
27         datalines;

NOTE: Numeric values have been converted to character values at the places given by: (Line):(Column).
      26:17   
NOTE: The data set WORK.TEST has 1 observations and 2 variables.
NOTE: DATA statement used (Total process time):
      real time           0.02 seconds
      cpu time            0.00 seconds

Clean programs should never have automatic numeric type conversions.

Instead use

data test;
input price $10.;
price2=compress(price,,'kd');
datalines;
825,563.25
;
run;

or

data test;
input price comma10.2;
price2=compress(put(price,best.),,'kd');
datalines;
825,563.25
;
run;

I would even define proper length for character variables according to the expected (infile documentation!) range.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Valued Guide
Posts: 632

Re: Error while reading special characters

There are multiple ways to do this, but taking your example at face value.  Consider using the COMPRESS function to remove the 'extra' characters.

data have;
input value $12.;
datalines;
123,456.78
$123,456.789
a1s2d3.wd40
run;
data want;
   set have;
   x = input(compress(value,,'dk'),best.);
   put (_all_)(=);
   run;
☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 192 views
  • 0 likes
  • 4 in conversation