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

Hi,

I have to 2 different SAS programs that generate 2
different SAS tables named table1 and table2. Table1 and table2 codes below:

data table1;

length state $3 Chevy $7 GMC $7 Honda $7 miles $20;

infile datalines dlm=',' dsd;

input state Chevy GMC Honda miles;

return;

datalines;

GA,$1200,$4300,$3000,"High Mileage Cars"   

;

run;

proc print data=table1;

run;

data Table2;

length Weeks $7 Prices $7 desc $32;

infile datalines dlm=',' dsd;

input Weeks Prices desc ;

return;

datalines;

' ',' ',"Insurance Track"

First,Chevy,"Old Cars without Insurance"

second,GMC,"Old Cars with Insurance"

Third,Honda,"Old Cars with Insurance"

;

run;

proc print data=table2;

run;

I want to write a program that would enable me to retrieve the values of the 

columns Chevy, GMC, Honda, and miles, from table1 and place them at their respective
positions in table2. Here is the design below:

         Data table: Miles

_______________________________________________

Weeks       Prices          desc

_______________________________________________

                     Insurance Track

First       Chevy    OldCars without Insurance

second     GMC       Old Cars with Insurance

Third       Honda    OldCars with Insurance

_______________________________________________

 

But the final output should contain the below data for respectively Chevy, GMC, Honda, miles:

            Data table: High Mileage Cars

_______________________________________________

Weeks       Prices          desc

_______________________________________________

                      Insurance Track

First       $1200    Old Cars without Insurance

second     $4300     Old Cars with Insurance

Third       $3000    Old Cars with Insurance

______________________________________________

Variable PRICES would be going on the user interface and treated as a global variable.

Thanks for help

1 ACCEPTED SOLUTION

Accepted Solutions
Haikuo
Onyx | Level 15

Given your data as it, the simplest way I see would be leveraging VVALUEX().

data want;

retain Weeks Prices desc;

if _n_=1 then set table1;

set table2(rename=prices=_p);

prices=vvaluex(_p);

keep Weeks Prices desc;

run;

View solution in original post

6 REPLIES 6
LinusH
Tourmaline | Level 20

Your "Table" needs to be transposed so that each row has the combined key of State and Prices (?).

Then you just join the two tables.

Data never sleeps
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Alternatively you can use update in SQL with a subquery:

data table1;

  length state $3 Chevy $7 GMC $7 Honda $7 miles $20;

  infile datalines dlm=',' dsd;

  input state Chevy GMC Honda miles;

datalines;

GA,$1200,$4300,$3000,"High Mileage Cars"  

;

run;

data Table2;

  length Weeks $7 Prices $7 desc $32;

  infile datalines dlm=',' dsd;

  input Weeks Prices desc ;

datalines;

' ',' ',"Insurance Track"

First,Chevy,"Old Cars without Insurance"

second,GMC,"Old Cars with Insurance"

Third,Honda,"Old Cars with Insurance"

;

run;

proc sql;

  update TABLE2 A

  set PRICES=case when PRICES="Chevy" then (select CHEVY from TABLE1)

                  when PRICES="GMC" then (select GMC from TABLE1)

                  when PRICES="Honda" then (select HONDA from TABLE1)

                  else "" end;

quit;

MikeZdeb
Rhodochrosite | Level 12

Hi.  You got answers, so this is just a comment.  The two data steps could also be written ...

data table1;

infile datalines dsd;

input state :$2. (Chevy GMC Honda) (:comma.) miles :$20.;

datalines;

GA,$1200,$4300,$3000,"High Mileage Cars" 

;

data Table2;

infile datalines dsd;

input weeks :$7. prices :$7. desc :$32.;

datalines;

' ',' ',"Insurance Track"

First,Chevy,"Old Cars without Insurance"

second,GMC,"Old Cars with Insurance"

Third,Honda,"Old Cars with Insurance"

;

#1  Rather than repeat the variable names in both a LENGTH and INPUT statements, you can assign variable attributes with INFORMATS in the INPUT statement.  Since you are using LIST input, use a COLON modifier prior to the INFORMAT.  The COMMA informat for the three prices will ignore that $-sign and read the data as numeric.  If you need that $-sign later, assign a DOLLAR format to the variables.

#2  The DSD in the INFILE statement implies a comma delimiter so youi don't need the DLM option.

#3  You don't need a RETURN statement to cycle through the data step.

#4  You don't need a RUN statement following a data step that ends with a DATALINES file.

Haikuo
Onyx | Level 15

Given your data as it, the simplest way I see would be leveraging VVALUEX().

data want;

retain Weeks Prices desc;

if _n_=1 then set table1;

set table2(rename=prices=_p);

prices=vvaluex(_p);

keep Weeks Prices desc;

run;

Amen
Calcite | Level 5

Thanks Hai, questions for you.

Why do I get this note message? :      Argument to function vvaluex is not a valid variable name.

                                                             Invalid argument to function vvaluex at line

MikeZdeb
Rhodochrosite | Level 12

Hi.  This message ...

NOTE: Argument to function VVALUEX is not a valid variable name:  .

NOTE: Invalid argument to function VVALUEX at line 23 column 8.

Weeks=  Prices=  desc=Insurance Track state=GA Chevy=1200 GMC=4300 Honda=3000 miles=High Mileage Cars _p=  _ERROR_=1 _N_=1

occurs since for one of the observations in TABLE2, the value of the variable variable PRICES (renamed _P and used as the argument in the VVALUEX function) is MISSING.

You could modify the code (not sure what the RETAIN was for) ...

data want;

if _n_=1 then set table1;

set table2(rename=prices=_p);

if ^missing(_p) then prices=vvaluex(_p);

keep Weeks Prices desc;

run;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 1468 views
  • 6 likes
  • 5 in conversation