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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

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
  • 1 reply
  • 686 views
  • 0 likes
  • 2 in conversation