Help using Base SAS procedures

variables names by string

Reply
Contributor
Posts: 37

variables names by string

Hi,

Is there a way to create variable names using string from an observation. This is where I got with the code:

DATA SASDATA.BAD1_REPAIRED;

SET SASDATA.BAD1;

COUNT + (-(LAG(FLAG) EQ 'LAST')*COUNT) + 1;

DO FIELD&S. = TOTAL_TEXT (S = COUNT); END;

RUN;

I am trying to do this

INPUT:

total_text count

abcdef      1

adfgh        2

LAST        3

OUTPUT:

total_text count field1      field2      field3

abcdef      1      abcdef

adfgh        2                    adfgh

LAST        3                                 LAST

Please help. Thanks again for your help.

-Akber.

Contributor
Posts: 37

variables names by string

Posted in reply to akberali67

Need help please!!!!

Contributor
Posts: 23

variables names by string

Posted in reply to akberali67

Akber, a proc transpose should do it.

data x;

input total_text $ count;

datalines;

abcdef  1

adfgh    2

LAST    3

;

run;

proc transpose data = x out=tx(drop=_name_) prefix=field;  

by total_text notsorted;  

id count; 

var total_text;

run;

HTH,

Rich

Super User
Posts: 10,041

variables names by string

Posted in reply to akberali67

Here is array method.

data x;
input total_text $ count;
datalines;
abcdef  1
adfgh    2
LAST    3
;
run; 
proc sql;
 select max(count) into : n from x;
quit;
data want;
 set x;
 array field{&n} $ ;
 field{count}=total_text;
run;



Ksharp

Super User
Super User
Posts: 7,062

Re: variables names by string

Posted in reply to akberali67

From your example it does not look like you want to create variable names, instead you want to put text into a matrix of variables.

The only thing you need to calculate before writing the code is the maximum number of counts.

Perhaps if you explained more about WHY you would want to do this you might get a better answer.

data have ;

  input total_text :$32. count @@;

cards;

abcdef 1 adfgh 2 LAST 3

run;

proc sql noprint;

  select max(count) into :max from HAVE ;

quit;

data want ;

  set have;

  array field (&max) $32 ;

  field(count)=total_text;

run;

proc print; run;

      total_

Obs     text     count    field1    field2    field3

1     abcdef      1      abcdef

2     adfgh       2                adfgh

3     LAST        3                           LAST

Super Contributor
Posts: 276

variables names by string

Oh..

its looks very simple..

Great skill tom..

Contributor
Posts: 37

variables names by string

Hi,

The reason why I want to do this is because these are actually parts of line but some data system is outputting them into different lines. So I want to retain until you have LAST in the data. So, what it will do is create one line in the final dataset. But this works and is quite simple.

Thanks again.

-Akber.

Ask a Question
Discussion stats
  • 6 replies
  • 328 views
  • 3 likes
  • 5 in conversation