2

when I run the following code, I see that the number in my character variable gets shifted as a value

data test;
input names$  score1 score2;
cards;
        A1  80      95 
        A 2 80      95

;
run;

proc print data=test;
run;

leading to a output like the following

The SAS System

                   Obs    names    score1    score2

                    1      A1        80        95
                    2      A          2        80

How do I create a variable like "A 2" with space so that the 2 doesn't get shifted

Joe
  • 62,789
  • 6
  • 49
  • 67
Nathgun
  • 115
  • 1
  • 1
  • 8
  • You can either use the underscore character `'_'` or no space at all. – Sirmyself Jan 24 '18 at 16:16
  • @Sirmyself While I don't think that answers the question, if you are trying to answer please use the 'answer' box. – Joe Jan 24 '18 at 16:18
  • there's also the "half-width Hangul" character `'ᅠ'` wich is similar to a whitespace character, but actually is a different one. https://stackoverflow.com/questions/48270127/can-a-1-a-2-a-3-ever-evaluate-to-true#answer-48274520 – Sirmyself Jan 24 '18 at 16:19
  • @Joe it is a partial answer and I did not intend to make a full answer until I know for sure what the op wanted. This is why I just commented it. – Sirmyself Jan 24 '18 at 16:20
  • @Sirmyself Comments are for clarifying the question: they are not for answers, even partial ones. Clarifying would be asking them the information that you need to know what they wanted. I think you're entirely on the wrong track here anyway - OP is asking a SAS question about data input, not a question about adding different kinds of whitespace - but you could certainly ask that question if you like to be sure. – Joe Jan 24 '18 at 16:24

2 Answers2

1

Your problem is you're using space delimited data input. Is it truly space delimited, though, or is it columnar (fixed position)?

data test;
input names $ 1-4 score1 5-12 score2 13-20;
cards;
A1  80      95 
A 2 80      95
;
run;

If it's truly delimited and you're just not exactly replicating the data here, you have a few choices. You can use the & character to ask SAS to look for two consecutive spaces to be a delimiter, but your actual data doesn't have that correctly either - but it would look like so:

data test;
input names &$ score1 score2;
cards;
A1   80      95 
A 2  80      95
;
run;

Or if you truly have the issue here that you have some single spaces that are delimiters and some single spaces that are not, you'll have to work out some sort of logic to do this. The exact logic depends on your rules, but here's an example - here I look for that space, and assume that if it is there then there is exactly one more character, then I want to move everything down one so that I have a guaranteed double space now. This is probably not a good rule for you, but it is an example of what you might do.

data test;
input @;
if substr(_infile_,2,1)=' ' then do;  *if there is a space at spot two specifically;
  _infile_ = substr(_infile_,1,3)||' '||substr(_infile_,4);  *shift everything after 3 down;
end;
input names &$ score1 score2;
cards;
A1  80      95 
A 2 80      95
;
run;
Joe
  • 62,789
  • 6
  • 49
  • 67
0

If your input is fixed block, as suggested, and the NAMES field is 12 bytes, as suggested by the data, then you can use formatted input for NAMES.

data test;
length names $ 12  score1 score2 8;
input names $12. score1 score2;
names=trim(left(names));
cards;
    A1  80      95 
    A 2 80      95

;
run;