DATA Step, Macro, Functions and more

put([var],[format]) not working ??

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 128
Accepted Solution

put([var],[format]) not working ??

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


Accepted Solutions
Solution
‎09-07-2016 10:47 AM
Super User
Posts: 5,499

Re: put([var],[format]) not working ??

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


All Replies
Solution
‎09-07-2016 10:47 AM
Super User
Posts: 5,499

Re: put([var],[format]) not working ??

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

Frequent Contributor
Posts: 128

Re: put([var],[format]) not working ??

[ Edited ]
Posted in reply to Astounding

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?

Super User
Posts: 5,499

Re: put([var],[format]) not working ??

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.

Super User
Posts: 11,343

Re: put([var],[format]) not working ??

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;
Super Contributor
Posts: 308

Re: put([var],[format]) not working ??

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;
☑ This topic is solved.

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

Discussion stats
  • 5 replies
  • 425 views
  • 1 like
  • 4 in conversation