Help using Base SAS procedures

Removing Spaces from Character or Numeric variable

Reply
N/A
Posts: 0

Removing Spaces from Character or Numeric variable

I am concatenating two binary indicator (1 or 0) variables, and I would like them to join together with no spaces ie 10 00 11. Unfortunately the output (txt or dbf) always displays them 1 0 or 0 0. I have tried the trim function and formatting the variable as 1. or 2. Neither seems to work. Any suggestions????
Super Contributor
Super Contributor
Posts: 3,174

Re: Removing Spaces from Character or Numeric variable

Posted in reply to deleted_user
Best option is to share what code you have tried for a most meaningful and accurate response.

Scott Barry
SBBWorks, Inc.
Super Contributor
Super Contributor
Posts: 3,174

Re: Removing Spaces from Character or Numeric variable

For example, not having seen your code, if you are using a PUT statement in a DATA step, you must also code +(-1) to suppress the implicit blank (equivalent of back-up one space) between variables unless you have an explicit format length in the PUT statement for the SAS variable display.

Scott Barry
SBBWorks, Inc.
SAS Super FREQ
Posts: 8,866

Re: Removing Spaces from Character or Numeric variable

Or, to followup on Scott's hint -- if you were using the concatenate functions or the concatenate operator, there are several different ways of doing the concatenation of separate numbers into a text string. The key is making sure that you control the conversion from numeric to character before you concatenate the numbers together.

cynthia
[pre]
data testcat;
length string1 string2 string3 string4 $2;
infile datalines;
input a b c d e f g h;
string1 = catt(put(a,1.0),put(b,1.0));
string2 = trim(put(c,1.0))||trim(put(d,1.0));
string3 = compress(put(e,1.0)||put(f,1.0));
string4 = left(put(g,1.0))||left(put(h,1.0));
return;
datalines;
1 0 1 0 1 0 1 0
1 1 1 1 1 1 1 1
0 1 0 1 0 1 0 1
0 0 1 1 0 0 1 1
;
run;

ods listing;
proc print data=testcat;
title 'make a string from 0 and 1';
run;

[/pre]
Contributor
Posts: 43

Re: Removing Spaces from Character or Numeric variable

Posted in reply to deleted_user
Why not just create the binary you want and then put it into binaryw. format when you are ready to present it as a string. This at least keeps the option open of working with these numbers as actual binary

put(2 * ind1 + ind2,binary2.)
Contributor
Posts: 43

Re: Removing Spaces from Character or Numeric variable

Posted in reply to mftuchman
Sample code


data testcat;
drop i;
infile datalines missover;
input ind1-ind8 @;
input original_input $ 1-15;
array ind{8};
together = 0;
do i = 1 to 8;
together = 2 * together + ind{i};
end;
together_as_str = put(together,binary8.);

datalines;
1 0 1 0 1 0 1 0
1 1 1 1 1 1 1 1
0 1 0 1 0 1 0 1
0 0 1 1 0 0 1 1
;
run;

ods listing;
15 title 'Make a string from 0 and 1';
16 proc report data=testcat;
17 column original_input ('Format' together together=_a together = _b together_as_str);
18 define together / display 'Decimal';
19 define _a / 'As Binary' format=binary8.;
20 define _b / 'As Hex' format=hex2. width=3;
21 run;
SAS Super FREQ
Posts: 8,866

Re: Removing Spaces from Character or Numeric variable

Posted in reply to mftuchman
Hi:
Nice example! I picked Character solely because it was listed first in the Subject....without any other explanation of how the variable was going to be used, I fell back on the subject for extra info.

cynthia
N/A
Posts: 0

Re: Removing Spaces from Character or Numeric variable

Posted in reply to deleted_user
Thanks all for the advice, I was able to remove the space for the individual sets of two binary variables (1 0 to 10), but when using the put statement to save a txt file, I need to remove spaces from all 52 variables (each a set of two numbers 10 01 11).
Following is the put statement I have. I am able to remove the space with the +(-), but need to do it between each variable. This is not ideal with 52 variables, especially if I need to change them, so I have them listed in the put statement, but how do I specify no spaces between the list?

sorry if the code is sloppy, I have just started using SAS.

****Transpose for MARK encounter history dummy is each binary set ie 10, 00, 11);
proc transpose data=last prefix=dummy out=dummy;
by lowtag;
id locweek; idlabel locweek;
var dummy;

data final; set dummy; by lowtag;
drop _name_;
File 'F:\Deer Capt, Loc, Mort Data\DeerDB\MARK SAS\sasdata.txt';
Put '*/' lowtag +(-1) '/*' dummy2452-dummy2504 +(-);
run;
SAS Super FREQ
Posts: 8,866

Re: Removing Spaces from Character or Numeric variable

Posted in reply to deleted_user
Hi,
The issue is that your PUT statement is writing out the whole list of numbered variables -- each with a space and -then- executing the +(-1) -- so for your purposes, you need to move to a different solution that would write out each dummy variable one by one.
[pre]
Put '*/' lowtag +(-1) '/*' dummy2452 +(-1) dummy2453 +(-1) .... dummy2504 ;
[/pre]

Well, it's sort of tedious to code all that, so there is another way. It involves treating your set of "dummy" variables as though they were in an array.

You probably also need to use the @ on the PUT statement to hold the text being built in the flat file, sasdata.txt -- by "hold the text" I mean hold the PUT pointer at a particular place in the current line of text being written -- this allows one variable to be written, the pointer moves back by -1, and then instead of writing the line to the output file, the line is held for the next PUT statement.

By using this form of the PUT statement in a DO loop, it allows your program to loop through the array in order to write each "dummy" variable to the line of text. Then, when the counter is 52, the line is released with the PUT;

So you'd need something like this (assumes your variables are character):
[pre]
data final;
set dummy;
by lowtag;
drop _name_;
array dm $ dummy2452-dummy2504;
n = -1;
File 'sasdata.txt';
Put '*/' lowtag +n '/*' @;
do i = 1 to 52 by 1;
put dm(i) +n @;
if i = 52 then put; /* might need some other condition to end the put */
end;
run;
[/pre]

cynthia
Ask a Question
Discussion stats
  • 8 replies
  • 6868 views
  • 0 likes
  • 4 in conversation