Use selected observations to calculate new variable

Accepted Solution Solved
Reply
Contributor
Posts: 33
Accepted Solution

Use selected observations to calculate new variable

[ Edited ]

Dear all, 

 

I want to use the jth, mth, rth,and sth observations to calculate new variables called val1 and val2. The following is my current code and I need help in the last program. The values of j,m,r and s will change for every different data. Can you please tell me what I should do in the last program? Or do you know of any solution for the last program? Thanks for your help. 

 

%LET a=0.6;
%LET nn=15;
data sample; */(arranged in ascending order);
input y;
datalines;
3
3
4
5
6
7
7
8
9
12
15
16
16
16
18
;
run;

 

data quanti;
p1=&a/2;
p2=1-p1;
np1=&nn* p1;
if np1<1 then np1=1;
else np1=np1;
fnp1=floor(np1);
np2=&nn* p2;
if np2<1 then np2=1;
else np2=np2;
fnp2=floor(np2);
run;

 


%LET j= ;
%LET m= ;
%LET r= ;
%LET s= ;
data want;
set quanti;
j= fnp1;
m= fnp2;
r=fnp1+1;
s=fnp2+1;
run;

 

 

data want2;
do _n_ = &j, &m, &r, &s; */The values of _n_ are supposed to be 4, 10,5,11;
set sample point=_n_;
output;
end;
stop;
val1=(X&j*X&m)+(X&r/X&s); */My end goal is to get val1= (4th obs x 10th obs) + (5th obs/11 obs);

val2=X&j/X&r;         */My end goal is to get val2= (4th obs/5th obs);
run;


Accepted Solutions
Solution
‎09-25-2015 11:17 AM
PROC Star
Posts: 7,364

Re: Use selected observations to calculate new variable

You can accomplish what you want with much simpler code. e.g.:

 

%LET a=0.6;
%LET nn=15;
data want (keep=val1 val2); */(arranged in ascending order);
infile datalines eof=lastrec;
array x(&nn.);
retain x: fnp1 fnp2;
input y;
if _n_ eq 1 then do;
p1=&a/2;
p2=1-p1;
np1=&nn* p1;
if np1<1 then np1=1;
else np1=np1;
fnp1=floor(np1);
np2=&nn* p2;
if np2<1 then np2=1;
else np2=np2;
fnp2=floor(np2);
end;
x(_n_)=y;
return;
lastrec:
do;
val1=X(fnp1)*X(fnp2)+(X(fnp1+1)/X(fnp2+1)); */My end goal is to get val1= (4th obs x 10th obs) + (5th obs/11 obs);
val2=X(fnp1)/X(fnp1+1); */My end goal is to get val2= (4th obs/5th obs);
output;
end;
datalines;
3
3
4
5
6
7
7
8
9
12
15
16
16
16
18
;
run;

View solution in original post


All Replies
Solution
‎09-25-2015 11:17 AM
PROC Star
Posts: 7,364

Re: Use selected observations to calculate new variable

You can accomplish what you want with much simpler code. e.g.:

 

%LET a=0.6;
%LET nn=15;
data want (keep=val1 val2); */(arranged in ascending order);
infile datalines eof=lastrec;
array x(&nn.);
retain x: fnp1 fnp2;
input y;
if _n_ eq 1 then do;
p1=&a/2;
p2=1-p1;
np1=&nn* p1;
if np1<1 then np1=1;
else np1=np1;
fnp1=floor(np1);
np2=&nn* p2;
if np2<1 then np2=1;
else np2=np2;
fnp2=floor(np2);
end;
x(_n_)=y;
return;
lastrec:
do;
val1=X(fnp1)*X(fnp2)+(X(fnp1+1)/X(fnp2+1)); */My end goal is to get val1= (4th obs x 10th obs) + (5th obs/11 obs);
val2=X(fnp1)/X(fnp1+1); */My end goal is to get val2= (4th obs/5th obs);
output;
end;
datalines;
3
3
4
5
6
7
7
8
9
12
15
16
16
16
18
;
run;

Contributor
Posts: 33

Re: Use selected observations to calculate new variable

Hi Art297, Thanks a bunch! This works when the data is written under "datalines" statement. 

What if the only information that we know is that there is only one numeric variable in the dataset and we do not know the name of that variable? The name of the variable is unknown because for every run of the code, I may be using a different dataset with only one variable (which is numeric). 

 

I am currently finding the solution right now, and I will post my current code later. 

 

Thanks..

 

 

 

 

 

Super User
Posts: 17,912

Re: Use selected observations to calculate new variable

Write a macro where you can pass the dataset name. Use either SASHELP.VCOLUMN to grab the column name and create a macro variable to embed in your code.

%macro automate(dset=);
*Get variable name;
proc sql noprint;
select name into :vname from sashelp.vcolumn where memname=upper(&dset) and libname='WORK';quit;

*rest of code using &vname macro variable;

%mend;
Contributor
Posts: 33

Re: Use selected observations to calculate new variable

Hi Art297, Thanks a bunch! This works when the data is written under "datalines" statement. 

What if the only information that we know is that there is only one numeric variable in the dataset and we do not know the name of that variable? The name of the variable is unknown because for every run of the code, I may be using a different dataset with only one variable (which is numeric). 

 

I am currently finding the solution right now, and I will post my current code later. 

 

Thanks..

Contributor
Posts: 33

Re: Use selected observations to calculate new variable

[ Edited ]

Alright. After modifying Art297's SAS code, I did not get my target. My goal is to get val1 and val2 from a data with one numeric variable and the name of the numeric variable is unknown (it may change for every data being analyzed). I also cannot paste the values of the data in the code since the values will change for different datasets being analyzed. The only known information is the name of the dataset.

 

Please help.  Thanks! 

 

 

Super User
Posts: 17,912

Re: Use selected observations to calculate new variable

[ Edited ]

The datalines are only to provide sample data. 

You can SET your dataset instead of having datalines.

 

There are many ways to find the name of the variable in a dataset.

Here's one, assuming that the dataset is HAVE in the WORK library and the variable name is stored in a macro variable var_name.
Check the log for the value of the variable.

 

Name of dataset=WORK.HAVE

 

 

 

proc sql;

select name into :var_name 

from sashelp.vcolumn

where libname=upper('WORK') and memname=upper("HAVE");

quit;

 

%put &var_name;

Contributor
Posts: 33

Re: Use selected observations to calculate new variable

Thanks Reeza! I used my former code but i put %global j m r s;

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 371 views
  • 0 likes
  • 3 in conversation