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

Hello everybody,

 

I will ask a question which can be little bit difficult to understand the logic. I have two different variables one of them includes four values and the other one includes three values. I need to create my desired table by using values with SAS code( You can see my desired data set in bottom). I know I can easily do it with manual( I can type the values in Excel and then Import to SAS) but I need to automatize this process.

 

I am not sure I can do it without Macro with multiple steps(PROC SQL or DATA STEP) or I am not sure how can I do it with macros but I tried to do it macros, I think I am close to do it, I would be so happy, if somebody can help me about it?

 

%LET Variable1=%STR("SECOND" "THIRD" "FOURTH");
%LET Variable2=%STR("COST" "MAIN" "AMOUNT" "SALARY");
%PUT &Variable1.;
%PUT &Variable2.;
DATA WANT;
LENGTH INFO1 $ 32 INFO2 $ 32 INFO3 $ 32;
INFILE DATALINES MISSOVER;
INPUT INFO1 INFO2 INFO3 ;
DATALINES;
FIRST COST COST
FIRST MAIN MAIN
FIRST AMOUNT AMOUNT
FIRST SALARY SALARY
RUN;

%MACRO INFO_GNR(Var1,Var2);
DATA HAVE;
LENGTH INFO1 $ 32 INFO2 $ 32 INFO3 $ 32;
INFO1=&Var1.;
INFO2=&Var2.;
INFO3=&Var2.;
RUN;

PROC APPEND DATA=HAVE BASE=WANT;
RUN;
%MEND INFO_GNR;
%MACRO LOOP_INFO_GNR;
%LET I=1;
%DO %WHILE(%SCAN(&Variable1.,&I.,%STR( ))~=);
%INFO_GNR(%SCAN(&Variable1.,&I.,%STR( )),%SCAN(&Variable2.,&I.,%STR( )));
%LET I=&I.+1;
%END;
%MEND LOOP_INFO_GNR;
%LOOP_INFO_GNR;

 

 

 

 

 

Desired Output;

Desired.png

 

 

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

Why not:

 

data want;
length info1-info3 $8;
do info1 = "FIRST", "SECOND", "THIRD", "FOURTH";
    do info2 = "COST", "MAIN", "AMOUNT", "SALARY";
        info3 = info2;
        output;
        end;
    end;
run;
PG

View solution in original post

1 REPLY 1
PGStats
Opal | Level 21

Why not:

 

data want;
length info1-info3 $8;
do info1 = "FIRST", "SECOND", "THIRD", "FOURTH";
    do info2 = "COST", "MAIN", "AMOUNT", "SALARY";
        info3 = info2;
        output;
        end;
    end;
run;
PG

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 1 reply
  • 1007 views
  • 3 likes
  • 2 in conversation