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

Hi all,

 

I ran a frequency and now i want to create a second table off the first table and make the numeric count into a character count so that i can suppress low turnout values with an asterisk.  Originally, i tried input(), since i was going from numeric to character.  In end, i tried all 4 options.

 

I run this one, and I get the "numeric values converted to character" in the log, and the count2 values are now "." for everything that should have an actual value:

data test;
      length count2 $5;
      set junk.Part1a_count;
      if 1 <= count <= 10 then count2="*";
      else count2=input(count,8.);
      where zipcode ne "";
run;

 

I run this one, and I get the "numeric values converted to character" in the log, but no values in count2 where there should be values.  Just blanks.

data test;
      length count2 $5;
      set junk.Part1a_count;
      if 1 <= count <= 10 then count2="*";
      else count2=left(input(count,$5.));
      where zipcode ne "";
run;

 

I run this, get no errors, but no values in count2 where there should be values.  Just blanks:

data test;
      length count2 $5;
      set junk.Part1a_count;
      if 1 <= count <= 10 then count2="*";
      else count2=put(count,8.);
      where zipcode ne "";
run;

 

 

If i run this, I get a warning in the log that count has already been defined as numeric, but it works.
data test;
      length count2 $5;
      set junk.Part1a_count;
      if 1 <= count <= 10 then count2="*";
      else count2=left(put(count,$5.));
      where zipcode ne "";
run;

 

What's the code to make it work, but without the warning message?

 

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

Going back to your very first DATA step, this statement is incorrect:

 

else count2=input(count, 8.);

 

The way to convert COUNT to character is to use the PUT function.  Since COUNT2 is defined as having a length of 5, try:

 

else count2=put(count,5.);

 

That should get you most of the way there (possibly all the way there).

View solution in original post

5 REPLIES 5
Astounding
PROC Star

Going back to your very first DATA step, this statement is incorrect:

 

else count2=input(count, 8.);

 

The way to convert COUNT to character is to use the PUT function.  Since COUNT2 is defined as having a length of 5, try:

 

else count2=put(count,5.);

 

That should get you most of the way there (possibly all the way there).

MeganE
Pyrite | Level 9

It worked.  Though, without the error, that's the fourth one i tried!  Except i had a $ in there.  But count2 is character.  Shouldn't the 5 be a $5?

Astounding
PROC Star

No dollar sign needed ... the PUT function always returns a character string. 

 

Adding a $ or not depends on whether the incoming variable (COUNT in this case) is character or numeric.  Since it is numeric, don't add the dollar sign.

ballardw
Super User

If you have multiple variables that would have the same "low count" to suppress you may be better off using a custom format.

Then you just print the data using that format. The example below will show any value of 10 or less as * and anything else formats as best10. You could specify any other format such as F10.2 or comma8. that you might want the varaibles displayed with.

 

 

proc format library=work;
   Value belowten
   low - 10 = "*"
   other = [best10.];
run;

data example;
   do x= 1 to 20;
   output;
   end;
run;

proc print data=example;
   var x;
   format x belowten.;
run;
Loko
Barite | Level 11

Hello,

 

in the following code, you see no errors because you have defined the variable count2 with length 5 while in the function you have the formula: put(count,8.). Therefore the value is truncated. Change the length to 8 and it will work:

 

data test;
      length count2 $5;
      set junk.Part1a_count;
      if 1 <= count <= 10 then count2="*";
      else count2=put(count,8.);
      where zipcode ne "";
run;

While for the last submitted code you get the warning because you use character format to an already numeric defined variable within the put formula. Change the formula to

(put(count,5.))

 

data test;
      length count2 $5;
      set junk.Part1a_count;
      if 1 <= count <= 10 then count2="*";
      else count2=left(put(count,$5.));
      where zipcode ne "";
run;

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 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
  • 5 replies
  • 3519 views
  • 1 like
  • 4 in conversation