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
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.
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.
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 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;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
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.