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

Hello Community,

Question from a noob.

I have two variables that need to be compared. Variable x is the old variable and variable y the new one. 
The values in variable x partly have a leading zero and a different date format than the values in variable y. Other values may be ok. 
I created two new variables. Varnew1 holds all formatted  values with a leading zero. Varnew2 holds all formatted new date values. I used tranwrd. 
Is it now possible to replace the new values into Var x so that i can compare var x and var y?
Example:

 

Var x                                Var y             Varnew1              Varnew2

1                                      1                        

30

05                                    5                         5 

2017-09-14                     21076                                         21076

...

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
s_lassen
Meteorite | Level 14

Something like this?

data have;
  length Var_X Var_Y $10;
  input Var_X Var_Y;
cards;
1 1          
30 .
05 5           
2017-09-14 21076 
;run;

options datestyle=ymd;
data want;
  set have;
  length Varnew1 Varnew2 $10;
  if Var_X ne '' and Var_Y ne '' and Var_X ne Var_Y then do;
    if input(Var_Y,10.)=input(Var_X,?? anydtdte10.) then
      Varnew2=Var_Y;
    else if input(Var_Y,10.)=input(Var_X,10.) then
      Varnew1=Var_Y;
    end;
run;

I added the OPTIONS DATESTYLE=YMD because some dates, e.g. 2017-09-08 can be ambigous.

 

View solution in original post

1 REPLY 1
s_lassen
Meteorite | Level 14

Something like this?

data have;
  length Var_X Var_Y $10;
  input Var_X Var_Y;
cards;
1 1          
30 .
05 5           
2017-09-14 21076 
;run;

options datestyle=ymd;
data want;
  set have;
  length Varnew1 Varnew2 $10;
  if Var_X ne '' and Var_Y ne '' and Var_X ne Var_Y then do;
    if input(Var_Y,10.)=input(Var_X,?? anydtdte10.) then
      Varnew2=Var_Y;
    else if input(Var_Y,10.)=input(Var_X,10.) then
      Varnew1=Var_Y;
    end;
run;

I added the OPTIONS DATESTYLE=YMD because some dates, e.g. 2017-09-08 can be ambigous.

 

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 1 reply
  • 667 views
  • 0 likes
  • 2 in conversation