BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Lupacante
Calcite | Level 5

Suppose you've got a dataset with variables var1 and var2. When var1 is equal to a certain value, you want to take the value of var2 in the next row down.

How do you do that?

Thanks in advance,

Marco

1 ACCEPTED SOLUTION

Accepted Solutions
data_null__
Jade | Level 19
data next;
   set sashelp.class end=eof;
   if not eof then set sashelp.class(firstobs=2 keep=weight rename=(weight=_weight));
   if sex eq 'F' and not eof then weight=_weight;
   run;

View solution in original post

5 REPLIES 5
Amir
PROC Star

Hi,

Here's an example way to do it:

data _null_;

  set sashelp.class(rename=(height=var1 weight=var2));

  if get_var2 then

  do;

    call symputx('var2_value',var2);

    stop;

  end;

  if var1=57.3 then

    get_var2+1;

run;

%put var2_value=&var2_value;

regards,

Amir.

Lupacante
Calcite | Level 5

Amir:

it successfully creates a dataset, but how do I then get the value for var2 for a particular one of var1? The dataset just has an extra column 'var2' which is zero everywhere except for that particular value of var1.


Amir
PROC Star

Hi,

Do you want to create:

A) A data set with one row showing the var1 value and the next var2 value

B) A macro variable holding the value

C) Something else, please specify with an example.

I saw a post from you that has since been deleted saying that SAS did not recognise the macro variable reference, in that case try replacing:

call symputx('var2_value',var2);

with

call symputx('var2_value',var2,'G');

to make the scope of the variable global, if, for example, it is being created in a macro function, and is being referenced outside of that macro function.

Regards,

Amir.

data_null__
Jade | Level 19
data next;
   set sashelp.class end=eof;
   if not eof then set sashelp.class(firstobs=2 keep=weight rename=(weight=_weight));
   if sex eq 'F' and not eof then weight=_weight;
   run;
Keith
Obsidian | Level 7

data ds_view / view=ds_view;

merge ds1

      ds1 (firstobs=2 keep=var2 rename=(var2=nextvar2));

run;

data want (drop=nextvar2);

set ds_view;

if var1=1 then var1=nextvar2;

run;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 1604 views
  • 6 likes
  • 4 in conversation