The SAS Output Delivery System and reporting techniques

While looping in for loop, creating new variables with counter

Reply
Regular Contributor
Posts: 229

While looping in for loop, creating new variables with counter

data test_table ;

set edc_recipe;

Nr = countc(RecipeParameters_1,'=');

do L = 1 to Nr;

Row_L = scan(RecipeParameters_1,2,'=');

Row_L = substr(Row_L,1,length(Row_L) - 2);

end;

run;

how can i do this??

Frequent Contributor
Posts: 138

While looping in for loop, creating new variables with counter

Hi filip,

what are you trying to achieve.please elaborate.

Regards,

Manoj

Super User
Posts: 17,840

Re: While looping in for loop, creating new variables with counter

your probably looking for a type of array where you can do something like the following:

The number of row variables will change so SAS doesn't know how many to create which will be an issue, if you have an idea of the max, set it to that. Or use an earlier dataset to create a macro variable with the largest number of = sign.

Here's an example:

data _null_;

set edc_recipe end=eof;

retain max=0;

nr=countc(recipe_parameters_1, "=");

if nr>max then max=nr;

if end then call symput ('maxc', max);

run;

%put &maxc.;

data test_table ;

set edc_recipe;

Nr = countc(RecipeParameters_1,'=');

array row{&maxc}  row1-row&maxc;

do L = 1 to Nr;

Row(L) = scan(RecipeParameters_1,2,'=');

Row(L) = substr(Row_L,1,length(Row_L) - 2);

end;

run;

Super User
Posts: 9,681

Re: While looping in for loop, creating new variables with counter

What does your data look like? and What output do you want?

Ksharp

Regular Contributor
Posts: 229

Re: While looping in for loop, creating new variables with counter

This is an example: it is actually a table. 1= first row with cells divided by + signs.

1=Par+STEP1+STEP2+STEP3+STEP4+STEP5+2=Thick[nm]+<B>21</B>+2+3+<B>43</B>+<B>36</B>+3=Time[Sec]+6+7+8+9+10+4=Time[Sec]+11+<B>212</B>+13+<B>3214</B>+15+5=Time[Sec]+<B>1</B>+<B>2</B>+<B>3</B>+<B>4</B>+<B>444</B>+6=Thick[nm]+1231212312+1232323+232323+23232323+232323232+7=Time[Sec]+<B>12</B>+<B>21</B>+<B>12</B>+<B>12</B>+<B>12</B>+8=Thick[nm]+1212+121212+1212+1212+12121212

so my goal is in the end to create a sort of table in HTML to output. ( so the x= will become <tr>, the + <td>, ..)

but i don't know how many rows the table will have.. and this is only one row in my dataset.. so the number of tablerows is different for every datasetrow

Regular Contributor
Posts: 229

Re: While looping in for loop, creating new variables with counter

 

* How can i put this ni code style?

SAS Super FREQ
Posts: 8,743

Re: While looping in for loop, creating new variables with counter

Hi:

  Is there a reason you do not want to use ODS HTML in order to write the appropriate HTML TR/TD tags around your data??? If you do not want standard HTML 4.01 tags, such as are created with regular ODS HTML, you can use ODS PHTML or ODS CHTML or ODS HTML3 in order to create other "plainer" types of HTML tags.

cynthia

Regular Contributor
Posts: 229

Re: While looping in for loop, creating new variables with counter

can you give an example cynthia of this technique

SAS Super FREQ
Posts: 8,743

Re: While looping in for loop, creating new variables with counter

Hi:

 

  If you have a SAS dataset and you want or need to create HTML tags around the dataset, you can do a simple PROC PRINT inside an ODS HTML "sandwich":

     

ods html3 file='c:\temp\class_ht3.html' style=minimal;
ods html file='c:\temp\class_ht4.html' style=minimal;
ods chtml file='c:\temp\class_cht.html' ;
ods phtml file='c:\temp\class_pht.html' style=minimal;

proc print data=sashelp.class(obs=3);
  var name age height weight;
run;

ods _all_ close;

and that is one approach. The downside of the ODS/PROC PRINT approach is that ODS is treating your data as a "report" it has a certain set of tags and formats and styles that it uses for this report version of your data (as you can see in the attached screenshot #1).

On the other hand, the SAS XML Libname Engine can also be used to write very sparse HTML tags around your data columns. Every row/observation will have a TR tag and every column will get a TD tag. To use the SAS XML Libname Engine, you would code something like this:

libname xmlout xml 'c:\temp\class_htx.html' xmltype=html

        xmlmeta=schemadata;

    

data xmlout.class;

   set sashelp.class(obs=3);

   keep name age height weight;

run;

    

libname xmlout clear;

Although this second method does not have any explicit ODS "sandwich" syntax, behind the scenes, the XML Libname Engine is making use of ODS TAGSET templates to write the appropriate type of markup tags when I specify XMLTYPE=HTML. The HTML tags created by this second method produces very spare HTML without ANY style references at all (as shown in attached screenshot #2). I still do not have a good sense of what your data looks like. You posted something like this,

1=Par+STEP1+STEP2+STEP3+STEP4+STEP5+

2=Thick[nm]+21+2+3+43+36+

3=Time[Sec]+6+7+8+9+10+

4=Time[Sec]+11+212+13+3214+15+

5=Time[Sec]+1+2+3+4+444+

6=Thick[nm]+1231212312+1232323+232323+23232323+232323232+

7=Time[Sec]+12+21+12+12+12+

8=Thick[nm]+1212+121212+1212+1212+12121212+

with the line breaks removed and indicated that you wanted to change the '+' signs to TD tags???? Or something. It was very hard to understand whether you had started with a SAS dataset and then had gotten to this point, or whether you were trying to read in this as raw text, or??? That sounds like a data manipulation exercise -- turning a long text string into multiple variables, so that you can get SAS/ODS to write HTML tags as you want around each column. Without understanding more about your data, all I can say is that I used a subset of SASHELP.CLASS for my program, which contained 3 rows/obs and multiple columns per obs and the screen shots show the difference between my 2 methods of having SAS put the HTML tags around the rows and columns.

I suppose, you could parse out and substitute in HTML tags manually, but I prefer to let SAS do the heavy lifting.

cynthia


_1_cht_html_output_program_tags.jpg_2_show_xml_engine_html_tags.jpg
Super User
Super User
Posts: 6,501

While looping in for loop, creating new variables with counter

Is this just a data step issue?

If your "table" is in a text file check and see if it has line breaks between rows.

Then you can just read it in cell by cell.

data table ;
  infile 'table.txt' dlm='+' dsd ;
  length row col 8 cell $200 ;
  row+1;
  do col=1 by 1 until(cell=' ');
    input cell @ ;
    output ;
  end;
run;

If you do not have line breaks then you will need to add extra logic to see when to start a new "row".

If you have empty cells then you will need to add extra logic to figure out how many cells are in each "row".

Now you could transpose this and use ODS to print to HTML (see replies above) or you can easily generate HTML syntax yourself in a data step.

data _null_;
  file 'table.html' ;
  if _n_=1 then put '<table>';
  if eof then put  '</table>';
  do until (last.row) ;
    set table end=eof;
    by row ;
    if first.row then put '<tr>';
    put '<td>' cell +(-1) '</td>';
  end;
  put '</tr>';
run;
Ask a Question
Discussion stats
  • 9 replies
  • 332 views
  • 0 likes
  • 6 in conversation