BookmarkSubscribeRSS Feed
akberali67
Calcite | Level 5

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.

6 REPLIES 6
akberali67
Calcite | Level 5

Need help please!!!!

rtritz
Calcite | Level 5

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

Ksharp
Super User

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

Tom
Super User Tom
Super User

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

kuridisanjeev
Quartz | Level 8

Oh..

its looks very simple..

Great skill tom..

akberali67
Calcite | Level 5

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.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 1480 views
  • 3 likes
  • 5 in conversation