BookmarkSubscribeRSS Feed
BrahmanandaRao
Lapis Lazuli | Level 10
data ds;
set sashelp.cars;
if Make='volkswagan'  then call symput("Status",'Available');
else call symput("Status",'Not Avaialable');
run;
%put &Status.;

it shows wrong  output

9 REPLIES 9
andreas_lds
Jade | Level 19

Check the variable "Make" - i am 100% sure that it never has the value "volkswagan".

BrahmanandaRao
Lapis Lazuli | Level 10
IF NOT AVAILABLE IT SHOULD BE SHOWS IN NOT AVAILABLE IN LOG WINDOW AS PER THE IF ELSE CONDITION
ScottBass
Rhodochrosite | Level 12

@BrahmanandaRao wrote:
IF NOT AVAILABLE IT SHOULD BE SHOWS IN NOT AVAILABLE IN LOG WINDOW AS PER THE IF ELSE CONDITION

If you're using SAS EG or DMS, read the doc on the data step debugger.


Please post your question as a self-contained data step in the form of "have" (source) and "want" (desired results).
I won't contribute to your post if I can't cut-and-paste your syntactically correct code into SAS.
Amir
PROC Star

Hi @BrahmanandaRao ,

 

Are you sure the code you have posted to this forum is the code you are using?

 

When I try submitting the posted code I get a syntax error indicating what is wrong with the code.

 

Please copy the code you have posted and run it in SAS to see if you get anything different to what you were running yourself, then,  if necessary, edit the original question to show the code you are running.

 

If you are not receiving any error message then please post your log showing the code and log messages using the "insert code" icon.

 

 

Kind regards,

Amir.

gamotte
Rhodochrosite | Level 12

Hello,

 

Note that instructions in a data step are executed for each iteration of the data step.

Since the macrovariable name you declare in the call symputs are not observation dependant,

&status. will be overwritten at each iteration and will eventually be equal to the value

for the last observation of the dataset. If you really want the value for the last observation,

you don't need to read the whole dataset. See for instance :

https://communities.sas.com/t5/SAS-Programming/How-to-output-last-observation-of-a-SAS-data-set/td-p...

 

Alternatively, if you want to create one macrovariable per observation, youcan suffix their

names with the observation number :

call symputx(cats('statut_',_N_), Make);

Note that i used symputx instead of symput as it strips the string in argument before exporting it

into a macrovariable, which is generally what is wanted.

 

In your test, since you are comparing strings, you can upcase them to avoid rejection

because of case differences :

if upcase(Make)='VOLKSWAGEN' ...

 Edit : A last advice is to carefully check the spelling, which i should have done before writing 'WOLKSWAGEN' 😀

ScottBass
Rhodochrosite | Level 12

@BrahmanandaRao wrote:
data ds;
set sashelp.cars;
if Make='volkswagan'  then call symput("Status",'Available');
else call symput("Status",'Not Avaialable');
run;
%put &Status.;

it shows wrong  output


WHAT OUTPUT ARE YOU EXPECTING?


Please post your question as a self-contained data step in the form of "have" (source) and "want" (desired results).
I won't contribute to your post if I can't cut-and-paste your syntactically correct code into SAS.
Kurt_Bremser
Super User

Now that you have added the necessary THEN, the step will run, but only the result of the last observation will end up in the macro variable. Since you also have quite incorrect spelling of the compare string, this will always be

Not Avaialable
ballardw
Super User

@BrahmanandaRao wrote:
data ds;
set sashelp.cars;
if Make='volkswagan'  then call symput("Status",'Available');
else call symput("Status",'Not Avaialable');
run;
%put &Status.;

it shows wrong  output


And just what should be the right output???

I get "Not Avaialable"  (your spelling not mine) with the above code.

Since this leaves the STATUS as of the very last record in the input data set are you sure that is what you wanted?

The last make in the data set is Volvo so even without the incorrect spelling of "volkswagen" you will never get 'Available' as a result.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 9 replies
  • 1203 views
  • 3 likes
  • 7 in conversation