<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Automatic Generation of specific variable names in a DATA procedure in SAS Data Management</title>
    <link>https://communities.sas.com/t5/SAS-Data-Management/Automatic-Generation-of-specific-variable-names-in-a-DATA/m-p/370098#M11176</link>
    <description>&lt;P&gt;You will have to post the code for inspection.&lt;/P&gt;</description>
    <pubDate>Fri, 23 Jun 2017 19:12:35 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2017-06-23T19:12:35Z</dc:date>
    <item>
      <title>Automatic Generation of specific variable names in a DATA procedure</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Automatic-Generation-of-specific-variable-names-in-a-DATA/m-p/323561#M9328</link>
      <description>&lt;P&gt;I am transposing a huge dataset of patients' clinical data. Each row contains the information of an operation number, code and date in a single visit of one patient. I have a functioning code that transposes the operation number, code and date into variables.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The whole thing looks like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA mydata;
FORMAT OP_date DDMMYYP10.;
input id visit op$ OPcode$ OP_date DDMMYY10.;
datalines;
580 20 14 x5-135 13.04.2011
1663 1 1 x5-124 17.12.2004
1663 1 2 x5-344 17.12.2004
1663 2 1 x5-424 .
...... etc.
;
run;

PROC SORT DATA=mydata; 
BY ID visit OP; 
RUN;

DATA transC;
DO UNTIL (LAST.ID);
Retain ID V1OP1C V1OP1D V1OP2C etc ..;
FORMAT V1OP1D DDMMYYP10. V1OP2D DDMMYYP10. etc... ;
SET mydata; 
BY ID visit;
IF visit="1" AND OP="1" THEN DO; V1OP1C=OPcode; V1OP1D=OP_date;END;
IF visit="1" AND OP="2" THEN DO; V1OP2C=OPcode; V1OP2D=OP_date;END;
IF visit="2" etc ...
DROP visit OP OPcode OP_date;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like to generate the variable names automatically in the IF statement so I wouldn't have to write myself all the combinations of all Visit and OP and also automatically generate the names of the new varibales.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It should be something like this:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;The FORMAT statement:
FORMAT VX_OPYdate DDMMYYP10. etc... ;

The IF statement:
IF visit=X AND OP=Y THEN DO; VX_OPYcode=OPcode; VX_OPYdate=OP_datum; END;

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I have tried it this with arrays, but it didn't work.&amp;nbsp;Also my experience is limited, so any suggestions to other parts of the code are welcome &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in forward&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Ubai.&lt;/P&gt;</description>
      <pubDate>Tue, 10 Jan 2017 10:16:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Automatic-Generation-of-specific-variable-names-in-a-DATA/m-p/323561#M9328</guid>
      <dc:creator>Ubai</dc:creator>
      <dc:date>2017-01-10T10:16:21Z</dc:date>
    </item>
    <item>
      <title>Re: Automatic Generation of specific variable names in a DATA procedure</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Automatic-Generation-of-specific-variable-names-in-a-DATA/m-p/323596#M9329</link>
      <description>&lt;P&gt;For writing repeating code, the macro language provides the necessary toolset:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data mydata;
format OP_date DDMMYYP10.;
input id visit op$ OPcode$ OP_date DDMMYY10.;
datalines;
580 20 14 x5-135 13.04.2011
1663 1 1 x5-124 17.12.2004
1663 1 2 x5-344 17.12.2004
1663 2 1 x5-424 .
;
run;

proc sort data=mydata; 
by ID visit OP; 
run;

%macro repeat_code(max_v,max_o);
data transC;
format ID 10.
%do i = 1 %to &amp;amp;max_v;
  %do j = 1 %to &amp;amp;max_o;
  V&amp;amp;i.OP&amp;amp;j.C $8.
  V&amp;amp;i.OP&amp;amp;j.D DDMMYYP10.
  %end;
%end;
;
do until (last.ID);
  set mydata; 
  by ID;
%do i = 1 %to &amp;amp;max_v;
  %do j = 1 %to &amp;amp;max_o;
  if visit = &amp;amp;i and OP = "&amp;amp;j"
  then do;
    V&amp;amp;i.OP&amp;amp;j.C = OPcode;
    V&amp;amp;i.OP&amp;amp;j.D = OP_date;
  end;
  %end;
%end;
end;
drop visit OP OPcode OP_date;
run;
%mend;
%repeat_code(20,14)
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 10 Jan 2017 12:20:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Automatic-Generation-of-specific-variable-names-in-a-DATA/m-p/323596#M9329</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-01-10T12:20:41Z</dc:date>
    </item>
    <item>
      <title>Re: Automatic Generation of specific variable names in a DATA procedure</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Automatic-Generation-of-specific-variable-names-in-a-DATA/m-p/323635#M9330</link>
      <description>&lt;P&gt;Use two proc transposes and merge the data sets.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="http://www.ats.ucla.edu/stat/sas/modules/ltow_transpose.htm" target="_blank"&gt;http://www.ats.ucla.edu/stat/sas/modules/ltow_transpose.htm&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or arrays:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://www.ats.ucla.edu/stat/sas/modules/longtowide_data.htm" target="_blank"&gt;http://www.ats.ucla.edu/stat/sas/modules/longtowide_data.htm&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, it's rare that a wide format is more efficient than a long format. It's usually harder to calculate downstream indicators in my experience.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 10 Jan 2017 14:33:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Automatic-Generation-of-specific-variable-names-in-a-DATA/m-p/323635#M9330</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-01-10T14:33:09Z</dc:date>
    </item>
    <item>
      <title>Re: Automatic Generation of specific variable names in a DATA procedure</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Automatic-Generation-of-specific-variable-names-in-a-DATA/m-p/323654#M9332</link>
      <description>&lt;P&gt;To what end?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To make it&amp;nbsp;data driven&amp;nbsp;you could use PROC TRANSPOSE.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA mydata;
   input id visit op OPcode $ OP_date:DDMMYY10.;
   FORMAT OP_date DDMMYYP10.;
   datalines;
580 20 14 x5-135 13.04.2011
1663 1 1 x5-124 17.12.2004
1663 1 2 x5-344 17.12.2004
1663 2 1 x5-424 .
;;;;
   run;
proc summary nway completetypes;
   class visit op;
   output out=frame(drop=_:);
   run;
proc print;
   run;
proc print data=mydata;
   run;
data frameV / view=frameV;
   set frame mydata;
   run;
proc transpose data=frameV out=code(where=(not missing(ID))) prefix=V suffix=C delim=OP;
   by id;
   var opcode;
   id visit op;
   format visit op z2.;
   run;
proc print;
   run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;IMG title="xCapture.PNG" alt="xCapture.PNG" src="https://communities.sas.com/t5/image/serverpage/image-id/6649i32D02C88F98CE368/image-size/original?v=v2&amp;amp;px=-1" border="0" /&gt;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 10 Jan 2017 15:11:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Automatic-Generation-of-specific-variable-names-in-a-DATA/m-p/323654#M9332</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2017-01-10T15:11:11Z</dc:date>
    </item>
    <item>
      <title>Re: Automatic Generation of specific variable names in a DATA procedure</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Automatic-Generation-of-specific-variable-names-in-a-DATA/m-p/323659#M9335</link>
      <description>Thanks Kurt. It worked very well.&lt;BR /&gt;&lt;BR /&gt;Reeza, thank you. I have transposed my data using all these methods to practice different techniques. However I found mine the most suitable for what I'm planning to do.&lt;BR /&gt;&lt;BR /&gt;You might be correct. As I have said my data includes several rows for each patient and I would like to transpose the data to a wide format to calculate survival time to different end points in each patient. The way that the data is entered make it somewhat hard for me to write a program that do what I'm looking for without restructuring the data.&lt;BR /&gt;&lt;BR /&gt;You can help a lot if you give a reference where I can learn how conduct a kaplan-meier or a cox regression over several observations for each patient.&lt;BR /&gt;I can post a sample of the real data if you want.</description>
      <pubDate>Tue, 10 Jan 2017 15:18:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Automatic-Generation-of-specific-variable-names-in-a-DATA/m-p/323659#M9335</guid>
      <dc:creator>Ubai</dc:creator>
      <dc:date>2017-01-10T15:18:15Z</dc:date>
    </item>
    <item>
      <title>Re: Automatic Generation of specific variable names in a DATA procedure</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Automatic-Generation-of-specific-variable-names-in-a-DATA/m-p/323665#M9336</link>
      <description>&lt;P&gt;I remember that I had to do something quite similar when preparing data for Enterprise Miner.&lt;/P&gt;</description>
      <pubDate>Tue, 10 Jan 2017 15:34:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Automatic-Generation-of-specific-variable-names-in-a-DATA/m-p/323665#M9336</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-01-10T15:34:45Z</dc:date>
    </item>
    <item>
      <title>Re: Automatic Generation of specific variable names in a DATA procedure</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Automatic-Generation-of-specific-variable-names-in-a-DATA/m-p/323687#M9340</link>
      <description>&lt;P&gt;For survival analysis too? I would love to learn from the scipt. I would be very grateful if I can have a look at it.&lt;/P&gt;</description>
      <pubDate>Tue, 10 Jan 2017 15:57:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Automatic-Generation-of-specific-variable-names-in-a-DATA/m-p/323687#M9340</guid>
      <dc:creator>Ubai</dc:creator>
      <dc:date>2017-01-10T15:57:44Z</dc:date>
    </item>
    <item>
      <title>Re: Automatic Generation of specific variable names in a DATA procedure</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Automatic-Generation-of-specific-variable-names-in-a-DATA/m-p/323864#M9354</link>
      <description>&lt;P&gt;The macro basically looks like that:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro cumvars(crit);
retain
  count_&amp;amp;crit
  sum_&amp;amp;crit
;
if first.person_key
then do;
  count_&amp;amp;crit = 0;
  sum_&amp;amp;crit = 0;
end;
if critvar = "&amp;amp;crit"
then do;
  count_&amp;amp;crit + 1;
  sum_&amp;amp;crit + sumvalue;
end;
%mend;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This is of course just a basic template, in reality there were much more variables.&lt;/P&gt;
&lt;P&gt;Since I work in an insurance company, the criterion was the insurance class, and we summed values like overall premium, new or changed risks in last year/last two years/last three years, claims (sum and number), and so on,&lt;/P&gt;
&lt;P&gt;The data step then looks like&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data person_base;
set claims_contracts;
by person_key;
%cumvars(cl1)
%cumvars(cl2)
%cumvars(cl3)
....
if last.person_key then output;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;For further automation, the macro calls could be done automatically from a list of classes.&lt;/P&gt;</description>
      <pubDate>Fri, 23 Jun 2017 19:10:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Automatic-Generation-of-specific-variable-names-in-a-DATA/m-p/323864#M9354</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-06-23T19:10:31Z</dc:date>
    </item>
    <item>
      <title>Re: Automatic Generation of specific variable names in a DATA procedure</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Automatic-Generation-of-specific-variable-names-in-a-DATA/m-p/370050#M11174</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Dear Kurt,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;thanks for the code. It helped alot and I did few steps afterwards. However, I was tryign to improve the code and shorten unnecessary parts. You have used the FORMAT statement in the macro. It keeps giving me the following warning:&lt;/P&gt;&lt;DIV class="sasWarning focus-line"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class="sasWarning focus-line"&gt;WARNING: Multiple lengths were specified for the BY variable ID_F by input data sets and LENGTH, FORMAT, INFORMAT, or ATTRIB&amp;nbsp;statements. This might cause unexpected results.&lt;/DIV&gt;&lt;DIV class="sasWarning"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class="sasWarning"&gt;and when I try to delete the whoe statement I get an error:&lt;/DIV&gt;&lt;DIV class="sasWarning"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class="sasWarning"&gt;&lt;SPAN&gt;Statement is not valid or it is used out of proper order.&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV class="sasWarning"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class="sasWarning"&gt;&lt;SPAN&gt;In a previous step I have identified and formatted the variable ID.&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV class="sasWarning"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class="sasWarning"&gt;&lt;SPAN&gt;Do you explain how SAS is understanding this and why am I getting this error? Thanks in farword.&lt;/SPAN&gt;&lt;/DIV&gt;</description>
      <pubDate>Fri, 23 Jun 2017 17:55:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Automatic-Generation-of-specific-variable-names-in-a-DATA/m-p/370050#M11174</guid>
      <dc:creator>Ubai</dc:creator>
      <dc:date>2017-06-23T17:55:09Z</dc:date>
    </item>
    <item>
      <title>Re: Automatic Generation of specific variable names in a DATA procedure</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Automatic-Generation-of-specific-variable-names-in-a-DATA/m-p/370098#M11176</link>
      <description>&lt;P&gt;You will have to post the code for inspection.&lt;/P&gt;</description>
      <pubDate>Fri, 23 Jun 2017 19:12:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Automatic-Generation-of-specific-variable-names-in-a-DATA/m-p/370098#M11176</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-06-23T19:12:35Z</dc:date>
    </item>
    <item>
      <title>Re: Automatic Generation of specific variable names in a DATA procedure</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Automatic-Generation-of-specific-variable-names-in-a-DATA/m-p/370180#M11177</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It's the code you mentioned in your answer in this thread. In the macro code, third line you used a FORMAT statement. This is the statement I meant.&lt;/P&gt;</description>
      <pubDate>Fri, 23 Jun 2017 21:54:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Automatic-Generation-of-specific-variable-names-in-a-DATA/m-p/370180#M11177</guid>
      <dc:creator>Ubai</dc:creator>
      <dc:date>2017-06-23T21:54:56Z</dc:date>
    </item>
    <item>
      <title>Re: Automatic Generation of specific variable names in a DATA procedure</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Automatic-Generation-of-specific-variable-names-in-a-DATA/m-p/370181#M11178</link>
      <description>&lt;P&gt;It's missing a semi-colon.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;EDIT: nope, but I suspect that's where the change happened to create the error anyways - you likely missed a semi-colon in the conversion or it needs an extra one for some reason.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 23 Jun 2017 22:14:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Automatic-Generation-of-specific-variable-names-in-a-DATA/m-p/370181#M11178</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-06-23T22:14:55Z</dc:date>
    </item>
    <item>
      <title>Re: Automatic Generation of specific variable names in a DATA procedure</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Automatic-Generation-of-specific-variable-names-in-a-DATA/m-p/370198#M11179</link>
      <description>&lt;P&gt;Ok, I guess I found it out.&lt;/P&gt;&lt;P&gt;This ID variable in my code is a character variable that includes numbers as well. That's why it does have multiple lengths.&lt;/P&gt;</description>
      <pubDate>Fri, 23 Jun 2017 23:16:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Automatic-Generation-of-specific-variable-names-in-a-DATA/m-p/370198#M11179</guid>
      <dc:creator>Ubai</dc:creator>
      <dc:date>2017-06-23T23:16:06Z</dc:date>
    </item>
  </channel>
</rss>

