BookmarkSubscribeRSS Feed
Siddharth123
Obsidian | Level 7

Hi All,

I have a SAS dataset structured as :

Var1Var2Var3
A..
A..
A..
A116APR2010
A..
B..
B117MAY2010

B

..
B..

I want to create macro variables : first which counts the distinct number of values in Var1 (2 in this case - A &B) and for each value create a process which reads the data ONLY until Var2 = 1 for each value. Once this is done, then I would like to populate my VAR3 backwards, for example in above case it would be 16APR2010 for A and 17 May2010 for B so in total there should be 6 records in final output file.

Please help.

Kind regards

SK

1 REPLY 1
slchen
Lapis Lazuli | Level 10

data have;
input var1 $ var2 var3 :date9.;
format var3 date9.;
cards;
A    .    .
A    .    .
A    .    .
A    1    16APR2010
A    .    .
B    .    .
B    1    17MAY2010
B    .    .
B    .    .
;
run;

/*Distinct number of values in var1*/

proc sql;

select count(distinct var1) into: num from have;

quit;

%put #

data want;
do i=1 by 1 until(last.var1);
    set have;
    by var1;
    retain count;
    if var2=1 then count=i;
end;
do i=1 by 1 until (last.var1);
    set have;
    by var1;
    if i<=count then output;
    end;
format var3 date9.;
drop i count;
run;

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

Register now

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 1066 views
  • 0 likes
  • 2 in conversation