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

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):(Column).
462:33 463:25 463:31
NOTE: Variable coverage1 is uninitialized.

1 ACCEPTED SOLUTION

Accepted Solutions
TomKari
Onyx | Level 15

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

3 REPLIES 3
TomKari
Onyx | Level 15

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

Kurt_Bremser
Super User
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.

ballardw
Super User

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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

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