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

Hello, I don't have any ideas how to decide this problem, therefore I don't have any codes and log. So.

I have a variable, lets name it "s_raw". It contains character values, most of them are just dates with date9. format.

But there is also values contains letter 'U' (for example "UUUU202")

What I need to do, is to create new numeric variable "w_raw" containing all date9. value format as numbers, and If the first character is “U”, leave the numeric Value missing.

I need to do this in one data step. Please, help me Cat Frustrated

Thanks in advance. 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

@IGK22 wrote:
Well, there is no other values except date9. format and all others are UUUU*random numbers*.
Unfortunately, I can't suppress errors, and if I do input function, I get dots(.) all over the variable

The question mark will suppress the errors in the log.  A single period is the default way that SAS will print a missing numeric value.

 

Are you saying that NONE of the values are getting converted to valid dates?  Are you sure that the text in the character variable is actual in a format that is valid for the DATE informat?  Do the values have hyphens or other punctuation between the day month and year parts?  Make the width you use on the informat is long enough for the longest valid date string.  Does it have leading spaces?  Does it contain tabs or other invisible characters?

 

Example: 

97   data test;
98     input char $32.;
99     num1=input(char,date9.);
100    num2=input(char,??date9.);
101    format num1 num2 date9.;
102  cards;

NOTE: Invalid argument to function INPUT at line 99 column 8.
RULE:      ----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0
103        UUU123
char=UUU123 num1=. num2=. _ERROR_=1 _N_=1
NOTE: Mathematical operations could not be performed at the following places. The results of the operations have been set to missing values.
      Each place is given by: (Number of times) at (Line):(Column).
      1 at 99:8
NOTE: The data set WORK.TEST has 3 observations and 3 variables.

Notice how the error message on occurs for the INPUT() function that did not have the ?? before the format specification.

Also notice that reading only 9 characters from the first date string made it assume that the 20 represented the year within the century.

Obs    char                num1         num2

 1     UUU123                 .            .
 2     01-JAN-2018    01JAN1920    01JAN1920
 3     01jan18        01JAN2018    01JAN2018

View solution in original post

4 REPLIES 4
Tom
Super User Tom
Super User

Just use the INPUT() function with the DATE9. informat specification.

What about the values that don't start with the letter U but are also not valid date strings?

What other value could you set them to than missing?.

 

If you want to suppress errors messages prefix the informat with a question mark.  You can use two question marks in a data step.

w_raw = input(s_raw,??date9.);
format w_raw date9. ;
IGK22
Calcite | Level 5
Well, there is no other values except date9. format and all others are UUUU*random numbers*.
Unfortunately, I can't suppress errors, and if I do input function, I get dots(.) all over the variable
Tom
Super User Tom
Super User

@IGK22 wrote:
Well, there is no other values except date9. format and all others are UUUU*random numbers*.
Unfortunately, I can't suppress errors, and if I do input function, I get dots(.) all over the variable

The question mark will suppress the errors in the log.  A single period is the default way that SAS will print a missing numeric value.

 

Are you saying that NONE of the values are getting converted to valid dates?  Are you sure that the text in the character variable is actual in a format that is valid for the DATE informat?  Do the values have hyphens or other punctuation between the day month and year parts?  Make the width you use on the informat is long enough for the longest valid date string.  Does it have leading spaces?  Does it contain tabs or other invisible characters?

 

Example: 

97   data test;
98     input char $32.;
99     num1=input(char,date9.);
100    num2=input(char,??date9.);
101    format num1 num2 date9.;
102  cards;

NOTE: Invalid argument to function INPUT at line 99 column 8.
RULE:      ----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0
103        UUU123
char=UUU123 num1=. num2=. _ERROR_=1 _N_=1
NOTE: Mathematical operations could not be performed at the following places. The results of the operations have been set to missing values.
      Each place is given by: (Number of times) at (Line):(Column).
      1 at 99:8
NOTE: The data set WORK.TEST has 3 observations and 3 variables.

Notice how the error message on occurs for the INPUT() function that did not have the ?? before the format specification.

Also notice that reading only 9 characters from the first date string made it assume that the 20 represented the year within the century.

Obs    char                num1         num2

 1     UUU123                 .            .
 2     01-JAN-2018    01JAN1920    01JAN1920
 3     01jan18        01JAN2018    01JAN2018
Reeza
Super User

Maybe you’re in the same class?

 

https://communities.sas.com/t5/New-SAS-User/URGENT-HELP-PLEASE/m-p/517749

 


@IGK22 wrote:

Hello, I don't have any ideas how to decide this problem, therefore I don't have any codes and log. So.

I have a variable, lets name it "s_raw". It contains character values, most of them are just dates with date9. format.

But there is also values contains letter 'U' (for example "UUUU202")

What I need to do, is to create new numeric variable "w_raw" containing all date9. value format as numbers, and If the first character is “U”, leave the numeric Value missing.

I need to do this in one data step. Please, help me Cat Frustrated

Thanks in advance. 




 

 

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 4 replies
  • 2062 views
  • 0 likes
  • 3 in conversation