Solved
Contributor
Posts: 60

# INFILE TXT NUMBER FOLLOW BY NEGATIVE SIGN

Hi,

new to SAS programming, tried to import raw data but how to convert the number follow with negative sign to negative number as the output missing value.

tried tp find solution but unable to solve it(believe wrong method), appreciated if you could spend sometime to guide me.

DATA TESTING;
INFILE DATALINES DLM='|' MISSOVER;
INPUT H :\$ G :\$ K;
IF MISSING(K) THEN
K=CATX('-',SUBSTR(K,1,LENGTH(K)-1));

DATALINES;
D    | ABC
E    | EFB    | 456
f    | WWL    | 567-
;

Accepted Solutions
Solution
‎03-16-2017 12:47 PM
Super User
Posts: 9,599

## Re: INFILE TXT NUMBER FOLLOW BY NEGATIVE SIGN

```data testing;
infile datalines dlm='|' missover;
input h \$ g \$ k \$;
k_num=input(cats(ifc(index(k,"-")>0,"-",""),tranwrd(k,"-","")),best.);
datalines;
D    | ABC
E    | EFB    | 456
f    | WWL    | 567-
;
run;```

You will see several things from the above.  First, I have used a code window ({i} above where you post) this distinguishes code from text.  Secondly I use consitent lower case, and 2 spaces indentation in my code for readablitiy.  Finally, I use the input() function which takes character and converts to number.  First I decide if there is a "-" in the text, if so its negative, and concatenate a miinus with the number then input.

All Replies
Solution
‎03-16-2017 12:47 PM
Super User
Posts: 9,599

## Re: INFILE TXT NUMBER FOLLOW BY NEGATIVE SIGN

```data testing;
infile datalines dlm='|' missover;
input h \$ g \$ k \$;
k_num=input(cats(ifc(index(k,"-")>0,"-",""),tranwrd(k,"-","")),best.);
datalines;
D    | ABC
E    | EFB    | 456
f    | WWL    | 567-
;
run;```

You will see several things from the above.  First, I have used a code window ({i} above where you post) this distinguishes code from text.  Secondly I use consitent lower case, and 2 spaces indentation in my code for readablitiy.  Finally, I use the input() function which takes character and converts to number.  First I decide if there is a "-" in the text, if so its negative, and concatenate a miinus with the number then input.

Contributor
Posts: 60

## Re: INFILE TXT NUMBER FOLLOW BY NEGATIVE SIGN

thank you so much, it really help me a lot.
Posts: 3,852

## Re: INFILE TXT NUMBER FOLLOW BY NEGATIVE SIGN

[ Edited ]

RW9 wrote:
```data testing;
infile datalines dlm='|' missover;
input h \$ g \$ k \$;
k_num=input(cats(ifc(index(k,"-")>0,"-",""),tranwrd(k,"-","")),best.);
datalines;
D    | ABC
E    | EFB    | 456
f    | WWL    | 567-
;
run;```

You will see several things from the above.  First, I have used a code window ({i} above where you post) this distinguishes code from text.  Secondly I use consitent lower case, and 2 spaces indentation in my code for readablitiy.  Finally, I use the input() function which takes character and converts to number.  First I decide if there is a "-" in the text, if so its negative, and concatenate a miinus with the number then input.

There is an INFORMAT for that but you get the programming for job security award.

``````data testing;
infile datalines dlm='|' missover;
input h \$ g \$ k :Trailsgn.;
*k_num=input(cats(ifc(index(k,"-")>0,"-",""),tranwrd(k,"-","")),best.);
datalines;
D    | ABC
E    | EFB    | 456
f    | WWL    | 567-
g    | WWL    | -567
;;;;
run;
proc print;
run;``````

Super User
Posts: 9,599