DATA Step, Macro, Functions and more

Can we use a condition for multiple variables

Reply
Contributor
Posts: 40

Can we use a condition for multiple variables

How Can I use same conditon for multiple variable?

lets say

 

data test;

infile cards delimiter=',' dsd;

input     inv $ Date1 mdyampm. Date2 mdyampm. Date3 mdyampm. Date4 mdyampm.;

cards ;

101,09/11/2014 10:22:00 AM,02/02/1900 10:22:00 PM,10/1/2014 11:22:00 AM,9/11/2014 02:22:50 AM

102,09/01/2014 10:21:00 AM,02/02/1900 10:22:00 PM,10/1/1900 12:19:00 AM,9/11/1900 12:22:50 AM

103,10/11/1900 10:02:00 AM,12/02/1900 10:29:09 PM,10/1/2014 11:22:00 AM,9/11/2014 02:23:10 AM

;run;

data test2;

set test;

if date1 <= '1/1/1900'd then date2=.;

if date2 <= '1/1/1900'd then date2=.;

if date3 <= '1/1/1900'd then date2=.;

if date4 <= '1/1/1900'd then date2=.;

;

 

is there any way I can write same codition to multiple datetime variable in single line, actually I have more date variables in my task  ;

Any suggestionon FORMAT to get values like ,2/09/2015 20:22:00

 

Thank you in advance

Respected Advisor
Posts: 3,156

Re: Can we use a condition for multiple variables

[ Edited ]

Looks like you could just:

if datepart(min(of date1-date4)) <= '1jan1900'd then date2=.;
Regular Contributor
Posts: 212

Re: Can we use a condition for multiple variables

Hi mate,

 

Yes there is and it is very easy by setting a %do loop to store the number of your date columns.

 

data have;
infile datalines4;
input ID$     VISIT     value1     value2     value3;
datalines4;
A     1     5     7     9
A     2     .     .      .
A     3     .     .       .
A     4     10     5     3
A     5     .     .      .
B     1     11     2     6
B     2     .      .     . 
B     3     16     7     9
B     4     .      .     . 
B     5     18     20     5
;;;;;;
run;
%macro date(num);
data want;
set have;
  %do i =1 %to 3;
    if value&&i. = . then value&&i.=0;
  %end;
run;
%mend;
%date(3);

Run this code to see the syntax an to understand how it works.

 

Att

Contributor
Posts: 40

Re: Can we use a condition for multiple variables

Posted in reply to DartibaliRodrigo
Thank you. In some of my files date variable name may be like created_date, Modified_date, date_lastupdated like that. It may not be suffixed with consecutive numbers.
Regular Contributor
Posts: 212

Re: Can we use a condition for multiple variables

[ Edited ]

Nbonda, there is another way where it is very simple.

Create a macro where store all the column names that contains "date" with dictionary tables.

 

SASHELP.VCOLUMN

 

Do the following:

 

proc sql;
    select name into: all_date_columns separated by " and " from sashelp.vcolumn
    where memname = "The name of your table" and name contains "date";
quit;

The use this macro in the condition.

 

Here is a code to this exactly:

 

data test;
  input id  _id _id1 var_id;
  cards;
1 2 3 4
; 
run;

proc sql;
    select name into: all_date_columns separated by " and " from sashelp.vcolumn
    where memname = "TEST" and name contains "id" and libname = "WORK";
quit; 

data test2;
  set test;
  if &all_date_columns. <= 4 then n_var=10;
run;

 

Hope this helps

 

Att

Super User
Super User
Posts: 7,942

Re: Can we use a condition for multiple variables

Yes, its called array processing.  Its good to see that your variables are prefix with a suffix number, this is good forma and makes working with the data easier.  Try something along the lines of:

data test;
inv=101; Date1="09NOV2014 10:22"dt; date2="02FEB1900 19:00"dt; date3="01JAN1890 11:22"DT; date4="09NOV2014 02:22"dt;
format date1-date4 datetime.;
run;

data want (drop=i);
set test;
array date{*} date:;
do i=1 to dim(date);
if datepart(date{i}) <= "01JAN1900"d then date{2}=.;
end;
run;

Note that this logic will mean if any of those dates are less than or equal to 01Jan1900 then the second date will be .

Ask a Question
Discussion stats
  • 5 replies
  • 327 views
  • 1 like
  • 4 in conversation