BookmarkSubscribeRSS Feed
nbonda
Obsidian | Level 7

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

5 REPLIES 5
Haikuo
Onyx | Level 15

Looks like you could just:

if datepart(min(of date1-date4)) <= '1jan1900'd then date2=.;
DartRodrigo
Lapis Lazuli | Level 10

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

nbonda
Obsidian | Level 7
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.
DartRodrigo
Lapis Lazuli | Level 10

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

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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 .

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
  • 5 replies
  • 3641 views
  • 1 like
  • 4 in conversation