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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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