BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
lydiawawa
Lapis Lazuli | Level 10

I have a dataset such as the following:

 

  login_loading_time    check_closing_time    login_closing_time    check_loading_time
400 415 456 421 405 214 413 411 410 412 400 403 450 423 451 401 454 411 441 421 458 401 433 411 750 410 412 410 760 710 451 421 770 798 413 433

 I'm interested in finding out the difference between xxx_loading_time and xxx_closing_time (xxx_loading_time - xxx_closing_time). Here xxx can be 'check' or 'login', and the outcome dataset should look like the following:

 

check_diff login_diff
  6             -56
 197            -8
  .              .
  .              .  

Is there a way to index terms in SAS arithmetic operation. What will be the best way to achieve the outcome?

 

 

Any help is much appriciated.

 

1 ACCEPTED SOLUTION

Accepted Solutions
gamotte
Rhodochrosite | Level 12

Hello,

 

Here is a solution where the code is generated by call execute's. It uses a loop to retrieve the names

of the dataset's columns with call vnext. Whenever the name contains "loading_time" it generates a line of code that

computes the corresponding difference. Note the "stop" at the end of the data step that prevents it

to read further than the first observation.

 

data have;
    input login_loading_time check_closing_time login_closing_time check_loading_time;
    cards;
400 415 456 421
405 214 413 411
410 412 400 403 
450 423 451 401
454 411 441 421
458 401 433 411
750 410 412 410
760 710 451 421
770 798 413 433
;
run;

data _NULL_;
    set have;
    length NAME $32.;

    call execute('data want; set have;');

    do while (NAME ne "NAME");
        call vnext(NAME);
        if find(upcase(NAME),"LOADING_TIME") then do;
            basename=scan(NAME,1,'_');
            call execute(cats(basename,'_diff=sum(',basename,'_loading_time,-',basename,'_closing_time);'));
			call execute('keep '||strip(basename)||'_diff;');
        end;
    end;
    call execute('run;');
    stop;
run;

 

View solution in original post

4 REPLIES 4
s_lassen
Meteorite | Level 14

One possibility is to use a macro:

%macro diff(prefixes);
  %local i w;
  %do i=1 %to %sysfunc(countw(&prefixes));
    %let w=%scan(&prefixes,&i);
    &w._diff=&w._loading_time-&w._closing_time;
    drop &w._loading_time &w._closing_time;
    %end;
%mend;

data want;
  set have;
  %diff(login check);
run;
gamotte
Rhodochrosite | Level 12

Hello,

 

Here is a solution where the code is generated by call execute's. It uses a loop to retrieve the names

of the dataset's columns with call vnext. Whenever the name contains "loading_time" it generates a line of code that

computes the corresponding difference. Note the "stop" at the end of the data step that prevents it

to read further than the first observation.

 

data have;
    input login_loading_time check_closing_time login_closing_time check_loading_time;
    cards;
400 415 456 421
405 214 413 411
410 412 400 403 
450 423 451 401
454 411 441 421
458 401 433 411
750 410 412 410
760 710 451 421
770 798 413 433
;
run;

data _NULL_;
    set have;
    length NAME $32.;

    call execute('data want; set have;');

    do while (NAME ne "NAME");
        call vnext(NAME);
        if find(upcase(NAME),"LOADING_TIME") then do;
            basename=scan(NAME,1,'_');
            call execute(cats(basename,'_diff=sum(',basename,'_loading_time,-',basename,'_closing_time);'));
			call execute('keep '||strip(basename)||'_diff;');
        end;
    end;
    call execute('run;');
    stop;
run;

 

ErikLund_Jensen
Rhodochrosite | Level 12

Hi @lydiawawa 

 

This seems to be exactly the same question as your other question "Performing operation by keywords in varaible name", also posted friday? - It was thoroughly discussed, and solutions given. I think they should be merged and closed.

lydiawawa
Lapis Lazuli | Level 10
@ErikLund_Jensen I'm not sure how the duplicated post was created. I will try to merge the two. Thanks for notifying!

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 4 replies
  • 763 views
  • 2 likes
  • 4 in conversation