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

hi I have two variables A and B . I wanted to compare those two variables based on date variable. on a particular date like 02/02/2019 if data is same then copy data before 02/0/2/2019 from A and 02/02/2/019 onwards  data from B and create new variable.

I am trying something like this.

data test;

set test_1;

length new_variable 8.;

if date='02FEB2019' then do;

if  A=B then new_variable = A;

end;

run;

1 ACCEPTED SOLUTION

Accepted Solutions
s_lassen
Meteorite | Level 14

I think what you want to do is something like this:

data test;
  set test_1;
  retain switch 0;
  if date='02FEB2019' and A=B then 
    switch=1;
  if switch then
    C=B;
  else
    C=A;
run;

View solution in original post

6 REPLIES 6
PaigeMiller
Diamond | Level 26

Try this

 

if date='02FEB2019'D then do;

IIf that doesn' t fix the problem, then please explain the problem in a lot more detail, and please show us a portion of your data set.

 
 
--
Paige Miller
SASUser0001
Fluorite | Level 6

it bring data for that date but I wanted to bring all the data from this date. but it gives me only for that particular day record like one record.

29

30 data test

31 set test_1

32 length C 8.

33

34 if date='31APR2002'd then do

35 if A =B then C=A

36 end

run

 

so like following if A and b have same data on 30/04/2002 then before 30/04/2002 data comes from A and after 30/

date                A          B

28-Feb-02  1236    5842

31-Mar-02  5662   553

30-Apr-02   3526  3526

30-Jun-02 3525 2060

30-Sep-02 3524 2041

31-Dec-02 3523 2022

31-Mar-03 3522 2003

30-Jun-03 3521 1984

30-Sep-03 3520 1965

31-Dec-03 3519 1946

 

 

 

 

 

 

SASUser0001
Fluorite | Level 6

and after 30/04/2002 comes from B and create variable C. sorry it didn't allow me to finish whole question in one

PaigeMiller
Diamond | Level 26

Please do NOT post code or log without semicolons. We have previously requested you not do this. Use the </> icon to post your log and use the "running man" icon to post code.

 

In your example, what should be done if A is not equal to B on the given date?

 

 
 
--
Paige Miller
SASUser0001
Fluorite | Level 6

sorry I didn't add semicolons as it gives me error when write message here.  if A=B then variable C should be created else c will be null. the way C should be created is before 30April2002 should come from A and after 30April2002 from B. thanks For helping

s_lassen
Meteorite | Level 14

I think what you want to do is something like this:

data test;
  set test_1;
  retain switch 0;
  if date='02FEB2019' and A=B then 
    switch=1;
  if switch then
    C=B;
  else
    C=A;
run;

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 6 replies
  • 887 views
  • 0 likes
  • 3 in conversation