SAS Data Integration Studio, DataFlux Data Management Studio, SAS/ACCESS, SAS Data Loader for Hadoop and others

How to replace the asterisk to a number "-99"?

Accepted Solution Solved
Reply
Super Contributor
Posts: 319
Accepted Solution

How to replace the asterisk to a number "-99"?

Hello:

 

I have the following dataset.   After I import them into SAS, the variable "coverage" become characterized.   I would like to replace the asterisk * with -99.  I wrote a quote below.  But one error message was shown in the log page,  It said that the "Coverage1" was uninitiated.  Why won't it happen?  How to resolve it?

 

Jurisdiction-Level Coverage
Alabama                  3
Alaska                     1
Arizona                    3
Arkansas                 3
California                 1

Colorado                 1
Connecticut             1
Delaware                 1
District of Columbia 1
Florida                     *
Georgia                   *

 

459 data test (drop=coverage);
460 length coverage $4.;
461 set test;
462 coverage=tranwrd(coverage1,'*','-99');
463 coverage=input(coverage1, 4.);
464 run;

NOTE: Numeric values have been converted to character values at the places given by:
(Line)Smiley SadColumn).
462:33 463:25 463:31
NOTE: Variable coverage1 is uninitialized.


Accepted Solutions
Solution
‎07-27-2016 04:02 PM
PROC Star
Posts: 1,167

Re: How to replace the asterisk to a number "-99"?

You can't have "Coverage" as a character and a numeric variable in the same data step.

 

What I do in this situation is rename the incoming variable in the "set" statement, something like:

 

set test(rename=(coverage=coverage_char));

 

and then

 

coverage_char=tranwrd(coverage_char,'*','-99');
coverage=input(coverage_char, 4.);

 

or

 

coverage=input(tranwrd(coverage_char,'*','-99'), 4.);

 

(untested)

 

Tom

View solution in original post


All Replies
Solution
‎07-27-2016 04:02 PM
PROC Star
Posts: 1,167

Re: How to replace the asterisk to a number "-99"?

You can't have "Coverage" as a character and a numeric variable in the same data step.

 

What I do in this situation is rename the incoming variable in the "set" statement, something like:

 

set test(rename=(coverage=coverage_char));

 

and then

 

coverage_char=tranwrd(coverage_char,'*','-99');
coverage=input(coverage_char, 4.);

 

or

 

coverage=input(tranwrd(coverage_char,'*','-99'), 4.);

 

(untested)

 

Tom

Super User
Posts: 7,868

Re: How to replace the asterisk to a number "-99"?

data test (drop=coverage);
/* the drop means that the variable "coverage" COMING FROM THE DATA STEP will not be included in the output dataset; this makes the whole data step useless */
length coverage $4.;
set test;
coverage=tranwrd(coverage1,'*','-99');
coverage=input(coverage1, 4.);
run;

You probably wanted this:

data test (drop=coverage1 coverage2);
set test (rename=(coverage=coverage1));
coverage2=tranwrd(coverage1,'*','-99');
coverage=input(coverage2, 4.);
run;

I used coverage2 because you can't rely on coverage1 (the old coverage) being long enough to hold the '-99' string.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Posts: 11,343

Re: How to replace the asterisk to a number "-99"?

Here is another approach using a custom informat.

proc format library=work;
invalue da
'*'=-99
other=_same_;
run;

data example;
   informat x $5.;
   input x ;
   coverage = input(x,da.);
datalines;
1
2
3
4
*
;
run;

You still need to have the resulting variable with a different name than that existing in you current data though.

 

If you were to end up having multiple codes you needed clean such as "**" then the Tranward approach requires multiple lines of code and order is going to be important (you wouldn't want to replace ** with -99-99 if the intent was ** maps to -999). The format approach would allow adding multiple replacements in the informat construction (not to mention multiple values to the same result) and only one line of code in the manipulation step.

☑ This topic is solved.

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

Discussion stats
  • 3 replies
  • 482 views
  • 2 likes
  • 4 in conversation