INFILE TXT NUMBER FOLLOW BY NEGATIVE SIGN

Accepted Solution Solved
Reply
Contributor
Posts: 32
Accepted Solution

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
Super User
Posts: 7,392

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.

View solution in original post


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

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: 32

Re: INFILE TXT NUMBER FOLLOW BY NEGATIVE SIGN

thank you so much, it really help me a lot.
Respected Advisor
Posts: 3,777

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;

Capture.PNG

Super User
Super User
Posts: 7,392

Re: INFILE TXT NUMBER FOLLOW BY NEGATIVE SIGN

Yes, didn't know about that informat.  As for job security, if I wanted that I would have created at least one macro, with a macro list, and an Excel paramters file Smiley Happy

☑ This topic is SOLVED.

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

Discussion stats
  • 4 replies
  • 188 views
  • 4 likes
  • 3 in conversation