BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
sagulolo
Quartz | Level 8

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-
;

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26
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

4 REPLIES 4
RW9
Diamond | Level 26 RW9
Diamond | Level 26
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.

sagulolo
Quartz | Level 8
thank you so much, it really help me a lot.
data_null__
Jade | Level 19

@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

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

Register now

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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