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

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

1 ACCEPTED SOLUTION

Accepted Solutions
andreas_lds
Jade | Level 19

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";

 

View solution in original post

3 REPLIES 3
andreas_lds
Jade | Level 19

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";

 

mkeintz
PROC Star

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:

  1. The filenames dslist and assigns are placed in a "TEMP" space.  They will be deleted  by SAS at the end of the session.
  2. The trailing "@" in the PUT statement tells sas to refrain from entering an end-of-line character after writing the value of DATASET.
  3. In the "DATA WANT;" step there are 2 %include statements.
    1. The first one has two trailing semi-colons.  The first if  for the %include statement. The second is because the included text does not contain a semi-colon.
    2. The  second %include needs only one semicolon, because the included text has complete statements -- with  their own semi-colons.
  4. Also there is no IF statement for the TERM="ACIDITY" condition, because the date variable defaults to a missing value.  There's no need to assign a  blank (I assume all the DTS variables are character variables).
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Reeza
Super User
I'd use the table to create a format and then use the new value with VVALUEX to obtain the correct date you want. If you know only one date will be filled there are other methods (COALESCE) that can be used instead.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 3 replies
  • 738 views
  • 4 likes
  • 4 in conversation