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;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 5 replies
  • 636 views
  • 6 likes
  • 4 in conversation