Hi All,
I have some datasets in one library and i want to create new dataset based on values collected in dataset.This dataset is actually a specification sheet and it contain around 30 rows and 10 variables.For Example, i have created below dataset.
data create;
input dataset $ 1-5 term $ 6-19 Variable $ 20-30;
datalines;
ABCD HYPERTENSION HYP_DTS
ABCD FEVER FEV_DTS
BCDA CANCER CAN_DTS
DCBA ACIDITY
;
My Requirement is to generate code as below
data new;
set abcd abcd bcda dcba;
if term="Hypertension" then date=HYP_DTS;
if term="FEVER" then date=FEV_DTS;
if term="CANCER" then date=CAN_DTS;
if term="ACIDITY" then date=" ";
run;
While doing above operation, i also need to consider source dataset where this term is collected.
Is there any possibility of doing this kind programming ?
Thanks in advance.
Regards,
Rajesh
Do you really want to duplicate the obs in [work.]abcd?
The code can be generated:
proc sql noprint;
select dataset into :datasetList separated by ' '
from create;
quit;
data _null_;
set work.create end=jobDone;
file "generated_code.sas";
length line $ 1000;
if _n_ = 1 then do;
put 'data new;';
put "set &datasetList.;";
end;
line = catx(' ', 'if term =', quote(trim(Term)), 'then date =', Variable, ';');
put line;
if jobDone then do;
put 'run;';
end;
run;
%include "generated_code.sas";
Do you really want to duplicate the obs in [work.]abcd?
The code can be generated:
proc sql noprint;
select dataset into :datasetList separated by ' '
from create;
quit;
data _null_;
set work.create end=jobDone;
file "generated_code.sas";
length line $ 1000;
if _n_ = 1 then do;
put 'data new;';
put "set &datasetList.;";
end;
line = catx(' ', 'if term =', quote(trim(Term)), 'then date =', Variable, ';');
put line;
if jobDone then do;
put 'run;';
end;
run;
%include "generated_code.sas";
I would suggest making 2 %INCLUDEable files, which I often find a bit simpler syntax than SQL:
data create;
input dataset $ 1-5 term $ 6-19 Variable $ 20-30;
datalines;
ABCD HYPERTENSION HYP_DTS
ABCD FEVER FEV_DTS
BCDA CANCER CAN_DTS
DCBA ACIDITY
;
filename dslist temp ;
filename assigns temp;
data _null_;
set create;
file dslist ;
put dataset @;
file assigns;
if variable ^=' ' then put 'if term =' term $quote20. 'then date=' variable ';';
run;
data want;
set
%include dslist /source2;;
%include assigns / source2 ;
run;
Notes:
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 25. 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.