BookmarkSubscribeRSS Feed
Kaushansky
Obsidian | Level 7

Thank you that works. Now what is I only wanted to count if a value is unique ONCE. Meaning, instead of how many unique, I only want to count 1 across all unique value.  Would prev(var1)=var1 work?

Tom
Super User Tom
Super User

@Kaushansky wrote:

Thank you that works. Now what is I only wanted to count if a value is unique ONCE. Meaning, instead of how many unique, I only want to count 1 across all unique value.  Would prev(var1)=var1 work?


So you no longer want to COUNT?  Instead you just want to FLAG the first occurrence of a value?

 

Just change how to assign values to the new variables.

 

Let's make some test data with more distinct values of the variables and try it.

data have ;
  input luid :$10. var1 :$10. var2 :$8. var3 :$8. ;
cards;
1027774874 6261781089 3g6gzrq 1744739
1027774874 6261781089 3g6gzrq 8888888
1027774874 6261781089 56vcsen 9999999
1027774874 9999999999 56vcsen 1744739
1027774874 6261781089 b9rq6w4 1744739
;

data want;
  set have ;
  array orig [3] $20 var1 var2 var3;
  array new [3] n_var1 n_var2 n_var3;
  retain n_var1 0 n_var2 0 n_var3 0;
  length value $20;
  if _n_=1 then do;
     declare hash h();
     h.definekey('index','value');
     h.definedata('index');
     h.definedone();
  end;
  do index=1 to dim(orig);
    value=orig[index];
    if h.add() then new[index]=0;
    else new[index]=1;
  end;
  drop index value;
run;

Results

Obs       luid          var1        var2       var3      n_var1    n_var2    n_var3

 1     1027774874    6261781089    3g6gzrq    1744739       1         1         1
 2     1027774874    6261781089    3g6gzrq    8888888       0         0         1
 3     1027774874    6261781089    56vcsen    9999999       0         1         1
 4     1027774874    9999999999    56vcsen    1744739       1         0         0
 5     1027774874    6261781089    b9rq6w4    1744739       0         1         0

Tom
Super User Tom
Super User

I cannot tell what you want.  Your picture seems to have proper FIRST. and LAST. variables if the BY statement has LUID followed by that DSP variable.  Whenever you are on the first observation for a value of LUID you are by definition on the first value of DSP for that particular value of LUID.

 

What is it that you want to create instead?

 

Note: If you want to have SAS calculate FIRST. and LAST. variables when the observations are not actually sorted by those same BY variables then you need to add the NOTSORTED keyword.

 

For example if you want to count how many times a patient changed DOSE you could sort the data by ID and DATE and then process the data by ID and DOSE with the NOTSORTED keyword and then use the FIRST.DOSE flag to detect when the dose changes.

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!

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
  • 17 replies
  • 2817 views
  • 4 likes
  • 4 in conversation